Php 无法使用CURL登录。。。但不知道为什么

Php 无法使用CURL登录。。。但不知道为什么,php,curl,Php,Curl,这是我的问题。几个月前,我写了一个PHP脚本来连接我在一个网站上的帐户。我用CURL连接,一切都很好。然后,他们更新了网站,现在我无法再连接。问题不在于CURL,因为我没有从CURL中得到任何错误,但网站本身告诉我我不能 这是我的剧本: <?php require('simple_html_dom.php'); //Getting the website main page $url = "http://www.kijiji.ca/h-ville-de-quebec/1700124";

这是我的问题。几个月前,我写了一个PHP脚本来连接我在一个网站上的帐户。我用CURL连接,一切都很好。然后,他们更新了网站,现在我无法再连接。问题不在于CURL,因为我没有从CURL中得到任何错误,但网站本身告诉我我不能

这是我的剧本:

<?php
require('simple_html_dom.php');


//Getting the website main page
$url = "http://www.kijiji.ca/h-ville-de-quebec/1700124";
$main = file_get_html($url);
$links = $main -> find('a');
//Finding the login page
foreach($links as $link){
    if($link -> innertext == "Ouvrir une session"){
        $page = $link;
    }
}
$to_go = "http://www.kijiji.ca/".$page->href;


//Getting the login page
$main = file_get_html($to_go);
$form = $main -> find("form");
//Parsing the page for the login form
foreach($form as $f){
    if($f -> id == "login-form"){
        $cform = $f;
    }
}
$form = str_get_html($cform);

//Getting my post data ready
$postdata = "";
$tot = count($form->find("input"));
$count = 0;

/*I've got here a foreach loop to find all the inputs in the form. As there are hidden input for security, I make my script look for all the input and get the value of each, and then add them in my post data. When the name of the input is emailOrNickname or password, I enter my own info there, then it gets added to the post data*/


foreach($form -> find("input") as $input){
    $count++;
    $postdata .= $input -> name;
    $postdata .= "=";
    if($input->name == "emailOrNickname"){
        $postdata.= "my email address ";
    }else if($input->name == "password"){
        $postdata.= "my password";
    }else{
        $postdata .= $input -> value;
    }
    if($count<$tot){
        $postdata .= "&";
    }
}

//Getting my curl session
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => $to_go,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $postdata,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_COOKIESESSION => true,
    CURLOPT_COOKIEJAR => 'cookie.txt'
));
$result = curl_exec ($ch);
curl_close ($ch);
echo $result;
?>

$form=$main->find(“form”)
查找元素的第一个匹配项

这就是


您需要将其更改为
$form=$main->find(“#登录表单”)

问题很可能是站点(服务器)检查cookie。这一过程主要包括两个阶段:

1) 当您在某个页面(例如登录页面)上首次访问站点时,服务器会使用一些数据设置cookie

2) 在随后的每个页面访问或POST请求中,服务器都会检查其设置的cookies

因此,您必须在脚本中重现此过程,这意味着您必须使用CURL从站点获取任何页面,包括应该通过CURL获取的登录页面,而不是
file\u get\u html


此外,您必须将
CURLOPT\u COOKIEJAR
CURLOPT\u COOKIEFILE
选项都设置为每个请求上相同的绝对路径值('cookies.txt'是相对路径)。这是必要的,以便在脚本将执行的整个请求系列(包括重定向)中启用Cookie自动处理(会话维护)。

您正在生成自己的postdata,但可能没有正确执行。curl可以接受一个数组。使
$postdata
成为一个key=value对数组,然后将整个数组传递给curl。感谢您的快速回答。不幸的是,我已经尝试将我的postdata作为数组,它也不起作用。您是否确保输入了所有字段,并且没有遗漏一些有趣的隐藏字段?尝试将推荐人设置为他们网站上的登录脚本谢谢Soundz的回答,我检查了网站上的所有字段,我都有。我的脚本甚至接受表单标记中的所有输入标记,因此应该不会有任何问题,除非表单可以与表单标记之外的其他字段一起发送。谢谢您的快速回答。不幸的是,我试过了,但似乎没有解决问题。事实上,当您使用
$form=$main->find(“form”)
进行解析时,它将返回所有。这就是为什么我会在之后使用foreach循环来找到正确的表单。哦,我没有看到下面的循环,但是我会直接使用#登录表单,它要快得多。不过,您应该尝试调试正在发送的/var_dump数组,可能有些信息没有正确发送。因此,请再次查看Hi Vertazzar,非常感谢您的时间。我尝试使用urlencode(),但仍然没有结果。我还尝试使用以下代码将我的帖子数据作为数组发送
$postdata=array();foreach($form->find(“input”)as$input){unset($to_-send);if($input->name==“emailOrNickname”){$to_-send=“我的电子邮件地址”;}否则if($input->name==“password”){$to-send=“我的密码”;}否则{$to-send=$input->value;}$postdata。再次非常感谢您抽出时间,Vertazzar服务器可能会检查Cookie(我看到他们有CSRF检查,这可能会在会话中存储CSRF值,然后与提交的值name=“ca.kijiji.xsrf.token”进行比较),请参阅下面的帖子,@hindsmost解释说,我相信您有什么发现。我觉得这很合乎逻辑。我会把我的脚本写出来,稍后再给你更新。