简单的CURL重定向

时间:2014-02-05 14:33:11

标签: redirect curl

我一直在这个网站寻找我的问题的解决方案,一些答案看起来很接近,但没有找到我的问题的解决方案 我有PHP curl登录到某个站点并在登录时重定向到某个链接。

***First to log:***

$ ch = curl_init();

     curl_setopt($ch, CURLOPT_URL, 'https  >somesite');
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_HEADER, 1);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100); 
     curl_setopt($ch, CURLOPT_COOKIESESSION, 1);
     curl_setopt($ch, CURLOPT_USERAGENT, $uagent);
     curl_setopt($ch, CURLOPT_REFERER, 'some other site');
     curl_setopt($ch, CURLOPT_TIMEOUT, 220);
     curl_setopt($ch, CURLOPT_FAILONERROR, 1);
     curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
     curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).DS.'cookies.txt');
     curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).DS.'cookies.txt');
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS,'userid='.$log.'&password='.$pass.);
     $content = curl_exec( $ch )

***then run this to get away from page with ads *** 

     $info = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 
     $fnd='ads'; ***// check to see if has ads***
     $pos=strpos($info, $fnd); 
    if ($pos!==false)
      {

$url1 = "https - link to redirect while logged id"; // ***I have this link for sure***
$ch1 = curl_init();
curl_setopt ($ch1, CURLOPT_URL, $url1);
curl_setopt ($ch1, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch1, CURLOPT_USERAGENT, $uagent);
curl_setopt( $ch1, CURLOPT_HEADER, 1); 
curl_setopt ($ch1, CURLOPT_COOKIESESSION, 0);
curl_setopt ($ch1, CURLOPT_TIMEOUT, 60);
curl_setopt ($ch1, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch1, CURLOPT_CONNECTTIMEOUT ,100); 
curl_setopt ($ch1, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch1, CURLOPT_COOKIEJAR, dirname(__FILE__).DS.'cookies.txt');
curl_setopt ($ch1, CURLOPT_COOKIEFILE, dirname(__FILE__).DS.'cookies.txt');
curl_setopt ($ch1, CURLOPT_POST, 0);

$content = curl_exec ($ch1);

但这不起作用。

谁愿意回答我?

谢谢。

1 个答案:

答案 0 :(得分:0)

我听到恐怖电影中的对话(不确定)很长一段时间“ Easy Pizzy,这意味着疯狂”。解决问题很容易,但直到你知道它: - )

关闭你的卷曲以确保它将cookie保存到文件中。否则它总是空的,因此不能进行二次呼叫。

// use it before second curl call
curl_close($ch);

事实上,在完成需求后关闭curl实例总是一个很好的做法!