【问题标题】:PHP cURL post fields not working correctlyPHP cURL 帖子字段无法正常工作
【发布时间】:2013-02-09 15:08:07
【问题描述】:

如果有人能帮我解决这个问题,那就太好了。 这就是我到目前为止所拥有的。当我运行它并回显 dom 内容时,我仍然会看到登录页面。但我看不出我做错了什么。

    $target_url = "https://secretsales.com/";
    $fields = array('email' => 'abc', 'password' => '123');
    $postFields = http_build_query($fields, '', '&');

    // make the cURL request to $target_url
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_URL, $target_url);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $html = curl_exec($ch);

    // parse the html into a DOMDocument
    $dom = new DOMDocument();
    @$dom -> loadHTML($html);

    // grab all the on the page
    $xpath = new DOMXPath($dom);

    echo $dom->saveHTML();

【问题讨论】:

  • 我只能在这里猜测,但我认为该站点需要在您登录之前设置一个 cookie。还可能有其他安全检查,例如 xsrf 令牌。

标签: php post curl http-post


【解决方案1】:

添加这个:

curl_setopt ($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/tmp/cookies.txt");
curl_setopt ($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/tmp/cookies.txt");

这会将 cookie 存储到指定的目录中,因此您可能必须创建该目录。

【讨论】:

  • 谢谢!你是对的。我也没有传递所有需要的参数。
猜你喜欢
  • 1970-01-01
  • 2014-07-30
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多