PHP Curl

Keep Cookie (keep login)
  1. define cookie tmp path (指定 tmp 位置
  2. set Cookiefile, Cookiejar,Cookiesession option (設定三個選項
  3. do not close curl (不要關閉 curl
$cookie = tmpfile();  //use php tmp
$userAgent = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31' ;
//create curl

$ch = curl_init();//=====request 1=====
$options = array(
CURLOPT_URL => $apiUrl,
CURLOPT_CONNECTTIMEOUT => 10000 ,
CURLOPT_USERAGENT => $userAgent,
CURLOPT_AUTOREFERER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_COOKIEFILE => $cookie,
CURLOPT_COOKIEJAR => $cookie,
CURLOPT_COOKIESESSION => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => 0,
);
curl_setopt_array($ch, $options);

$html = curl_exec($ch);
//curl_close($ch);  //don't close curl

//=====request 2=====
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
$data = curl_exec($ch);
curl_close($ch);        

PHP Curl PHP Curl Reviewed by Wild on 1/01/2016 04:32:00 下午 Rating: 5

沒有留言:

沒有Google帳號也可發表意見唷!

技術提供:Blogger.