1

I cannot resolve a redirect in curl with PHP. I call the URL https://data.fei.org/Horse/Search.aspx, but as result I get a login site. 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?

Marlowe
  • 252
  • 3
  • 15

1 Answers1

0

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. 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.

to verify, add more error checking (this is actually what you should have done in the first place, before even asking on 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.

on that same note, add error checking to curl_setopt too. if something went wrong setting the curl option curl_setopt returns 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 )

another possibility is that your prod server is ip-banned from logging in.

hanshenrik
  • 19,904
  • 4
  • 43
  • 89