简体   繁体   English

使用cURL php Cookie登录

[英]Login with cURL php Cookie

I need help to make a login like the one shown in the image. 我需要帮助来进行如图所示的登录。 (www.hurl.it) (www.hurl.it) hurl.it

This should be done in PHP with cURL. 这应该使用cURL在PHP中完成。 This is what I have so far but it does not work for me: 这是我到目前为止所拥有的,但对我不起作用:

$c = curl_init('https://www.mkr.cl/users/login?redirect=/store/product/22207');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_AUTOREFERER, 1 );
curl_setopt($c, CURLOPT_HEADER, 1);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($c, CURLOPT_POST,true);
curl_setopt($c, CURLOPT_POSTFIELDS, 'username=76696459-1&password=7669');
curl_setopt($c, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($c, CURLOPT_COOKIEFILE, 'cookies.txt');
$result = curl_exec($c);
curl_close($c);
print_r($result);

your code works correctly, and does in fact log in, when i testrun it here. 当我在此处进行测试时,您的代码可以正常工作,并且实际上可以登录。 i voted to close this question, because you just say "it does not work for me", without explaining HOW it does not work for you. 我投票结束了这个问题,因为您只是说“它对我不起作用”,而没有说明如何对您不起作用。

but, when debugging, don't use print_r , use var_dump . 但是,在调试时,请勿使用print_r ,而应使用var_dump for example, if curl gets an error, it returns bool(false) , and when you give bool(false) to print_r , it prints absolutely nothing, giving you no clue what happens. 例如,如果curl出错,则返回bool(false) ,而当您将bool(false)赋予print_r ,它绝对不打印任何内容,不提供任何提示。

however, if you give bool(false) to var_dump , it will in fact print bool(false). 但是,如果将bool(false)赋予var_dump ,则实际上将输出bool(false)。

also, when debugging curl code specifically, enable CURLOPT_VERBOSE , it prints lots of useful debugging info. 同样,在专门调试curl代码时,启用CURLOPT_VERBOSE ,它会打印出许多有用的调试信息。 and when curl_exec returns false , you should use curl_error() to extract the error message. curl_exec返回false时 ,应该使用curl_error()提取错误消息。

in short, replace $result = curl_exec($c); 简而言之,替换$result = curl_exec($c); with: 有:

curl_setopt($c,CURLOPT_VERBOSE,true);
$result = curl_exec($c);
if(false===$result){
throw new \RuntimeException('curl_exec failed! errno: '.curl_errno($c).'. error: '.curl_error($c));
}
var_dump($result);

additionally, if there's a problem setting any of your curl options, curl_setopt returns bool(false) , which you also completely ignore here, don't do that! 另外,如果在设置任何curl选项时遇到问题,curl_setopt返回bool(false),您在这里也将其完全忽略,不要这样做! use something like: 使用类似:

function ecurl_setopt ( /*resource*/$ch , int $option , /*mixed*/ $value ):bool{
    $ret=curl_setopt($ch,$option,$value);
    if($ret!==true){
        //option should be obvious by stack trace
        throw new RuntimeException ( 'curl_setopt() failed. curl_errno: ' .  curl_errno ($ch).'. curl_error: '.curl_error($ch) );
    }
    return true;
}

now, instead of silently ignoring setopt errors, an exception will be thrown if there's a problem applying your settings. 现在,如果您在应用设置时遇到问题,它会抛出一个异常,而不是默默地忽略setopt错误。 anyway, apply CURLOPT_VERBOSE , don't ignore setopt return value, and use var_dump instead of print_r , and if it still doesn't work, print the VERBOSE log, and whatever was printed by CURLOPT_VERBOSE and var_dump and curl_error , THEN i will retract my close vote. 无论如何,应用CURLOPT_VERBOSE ,不要忽略setopt返回值,并使用var_dump而不是print_r ,如果仍然不起作用,则打印VERBOSE日志,以及由CURLOPT_VERBOSEvar_dumpcurl_error打印的curl_error ,然后我将收回我的结束投票。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM