简体   繁体   English

PHP curl重定向到其他站点

[英]PHP curl redirect to a different site

I cannot resolve a redirect in curl with PHP. 我无法使用PHP解决curl中的重定向问题。 I call the URL https://data.fei.org/Horse/Search.aspx , but as result I get a login site. 我将URL称为https://data.fei.org/Horse/Search.aspx ,但最终得到一个登录站点。 This occurs not on all server. 并非在所有服务器上都发生这种情况。 In the test environment it works fin, but not production server. 在测试环境中,它可以正常运行,但不能在生产服务器上运行。 That's my curl init 那是我的卷发初始化

$url = "https://url.org/Search.aspx";
$checkFile = tempnam('/tmp', 'cookie.txt');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, "my.proxy.com:8080");
curl_setopt($ch, CURLOPT_PROXYPORT, 8080);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded'
));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $checkFile);
curl_exec($ch);

Any idea why I'm redirected on production but not on test environment? 知道为什么我重定向到生产环境而不是测试环境吗?

best guess: on the test server, you have write access to /tmp and can indeed create new files there, but on the production server, you don't have write access to /tmp. 最佳猜测:在测试服务器上,您具有对/ tmp的写权限,并且确实可以在其中创建新文件,但是在生产服务器上,您没有对/ tmp的写权限。 because you don't check the return type of tempnam, you give curl bool(false) to CURLOPT_COOKIEFILE on the production server , and curl is unable to save/load cookies, and fails to serve the cookies received in request #1 to the followup Location-redirect request #2 - because it has an invalid cookie file. 因为您不检查tempnam的返回类型,所以将curl bool(false)赋予生产服务器上的CURLOPT_COOKIEFILE,curl无法保存/加载cookie,并且无法将请求1中收到的cookie提供给后续服务位置重定向请求#2-因为它具有无效的cookie文件。

to verify, add more error checking (this is actually what you should have done in the first place, before even asking on stackoverflow) 进行验证,添加更多错误检查(实际上,这是您甚至应该询问stackoverflow之前应该做的事情)

$checkFile = tempnam('/tmp', 'cookie.txt');
if(false===$checkFile){
    throw new \RuntimeException('tmpnam failed to create cookie temp file!');
}

also protip, when debugging curl code, always enable CURLOPT_VERBOSE , it prints lots of useful debugging info, including all redirects, and all cookies sent with all redirects. 另外,在调试curl代码时,请始终启用CURLOPT_VERBOSE,它会打印许多有用的调试信息,包括所有重定向以及通过所有重定向发送的所有cookie。

on that same note, add error checking to curl_setopt too. 同样,将错误检查也添加到curl_setopt。 if something went wrong setting the curl option curl_setopt returns bool(false). 如果在设置curl选项时发生错误,curl_setopt返回bool(false)。 in my curl wrapper, i do 在我的卷发包装纸上

    $ret = curl_setopt ( $this->curlh, $option, $value );
    if (! $ret) {
        throw new InvalidArgumentException ( 'curl_setopt failed. errno: ' . $this->errno () . '. error: ' . $this->error () . '. option: ' . var_export ( $this->_curlopt_name ( $option ), true ) . ' (' . var_export ( $option, true ) . '). value: ' . var_export ( $value, true ) );
    }

( from https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php#L997 ) (来自https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php#L997

another possibility is that your prod server is ip-banned from logging in. 另一种可能性是您的产品服务器被IP禁止登录。

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

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