du42561 2015-03-09 09:38
浏览 92

在null上调用成员函数getNamedItem()

I'm trying to make a tool that will get the time someone needs to work. The login is secured with an CSRF token so I need to get that first before I can login.

This is the script what i'm using at the moment:

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

  $username = "jules.kreutzer";
  $password = "jules";
  $url = "planner.a-mac.nl";
  $cookie= "koekje.txt";
  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/'.$cookie);
  curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/'.$cookie);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);

  if (curl_errno($ch)) die(curl_error($ch));
  $doc = new DOMDocument();
  $doc->loadHTML("http://planner.a-mac.nl");
  $token = $doc->getElementByID("signin__csrf_token")->attributes->getNamedItem("value")->value;
  print_r($token);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  curl_setopt($ch, CURLOPT_POST, true);

  $params = array(
    'signin[username]' => $username,
    'signin[password]' => $password,
    'signin[_csrf_token]' => $token
  );
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

  curl_exec($ch);

  if (curl_errno($ch)) print curl_error($ch);
  curl_close($ch);
?>

When I load this code in my webbrowser I get the following errors:

Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/test.php on line 22

Fatal error: Call to a member function getNamedItem() on null in /Applications/MAMP/htdocs/test.php on line 22

line 22 is:

  $token = $doc->getElementByID("signin__csrf_token")->attributes->getNamedItem("value")->value;

I was hoping someone could help me get the token so I can use it for the login.

  • 写回答

1条回答 默认 最新

  • dtrphb5597 2015-03-09 09:45
    关注

    You misspelled getElementById:

    Change:

    $token = $doc->getElementByID("signin__csrf_token")->attributes->getNamedItem("value")->value;
    

    to:

    $token = $doc->getElementById("signin__csrf_token")->attributes->getNamedItem("value")->value;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作