CURL有關問題

來源:互聯網
上載者:User
CURL問題
問什麼下面這段代碼得不到正確的cookie?

$url = "http://www.edai365.cn/Index/login.aspx";
$useragent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; TheWorld)";
$cookie = "cnzz_eid=43361819-1349499907-&ntime=1349499907&cnzz_a=1&retime=1349499930944&sin=none&ltime=1349499930944&rtime=0";
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_REFERER, "http://www.edai365.cn/Index/login.aspx");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, $useragent);
curl_exec($ch);
$tempCn = curl_multi_getcontent($ch);
curl_close($ch);
echo $tempCn;


------解決方案--------------------
$cookie_jar="";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
指定一個cookie的儲存地址;
用的時候
加上curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar);

不知道符不符合你的要求
------解決方案--------------------
不知道你想幹什麼
http://www.edai365.cn/Index/login.aspx 是登陸頁面,核心是一個表單
如果你是想類比登陸,至少也需要類比提交的表單資料
------解決方案--------------------
我測試了一下,可以抓取到成功登入的資訊,至於為何進入登陸頁面我也沒有研究明白.
PHP code
// 類比請求資料Function request($url,$postfields,$referer='',$cookie_jar='./cookie.txt'){    $Cur = curl_init();    $Opt = Array(        CURLOPT_URL => $url,        CURLOPT_HEADER => 1,        CURLOPT_NOBODY => 1,        CURLOPT_PORT => 80,    //連接埠        CURLOPT_POST => 1,    //發送一個常規的POST請求,類型為:application/x-www-form-urlencoded,就像表單提交的一樣。        CURLOPT_POSTFIELDS => $postfields,//要傳送的所有資料,如果要傳送一個檔案,[email protected]        CURLOPT_RETURNTRANSFER => 1,// 擷取的資訊以檔案流的形式返回,而不是直接輸出。        CURLOPT_SSL_VERIFYHOST => 1,// 從認證中檢查SSL密碼編譯演算法是否存在        CURLOPT_SSL_VERIFYPEER => 0,// 對認證認證來源的檢查,0表示阻止對認證的合法性的檢查。        CURLOPT_COOKIE => GetCook($url),        CURLOPT_USERAGENT => "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)",//類比使用者使用的瀏覽器,在HTTP請求中包含一個"user-agent"頭的字串。        //CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1',        CURLOPT_FOLLOWLOCATION => 0,        CURLOPT_COOKIEJAR => Dirname(__FILE__).$cookie_jar,        CURLOPT_COOKIEFILE => Dirname(__FILE__).$cookie_jar,        CURLOPT_REFERER => $referer,    );    curl_setopt_array($Cur,$Opt);    $code = curl_exec($Cur);    curl_close($Cur);    Unset($Cur);    Return $code;}Function GetCook($url) {    $Curl=curl_init($url);    curl_setopt($Curl,CURLOPT_HEADER,1);    curl_setopt($Curl,CURLOPT_RETURNTRANSFER,1);    $Cook=curl_exec($Curl);    curl_close($Curl);    $Out='';    preg_match("/_SessionId\=(.[^;]*)/i",$Cook,$Out);    Return $Out[1];}$url = "http://www.edai365.cn/Index/login.aspx";$postfields='txt_nickname=baluwu&txt_pwd=000000&ddlcookietime='.UrlEncode('一周').'&ibt_login=';$a=request($url,$postfields);unset($postfields,$url);Echo "",Chr(10),$a,Chr(10),"";Die();
------解決方案--------------------
http://www.21andy.com/blog/20080507/1095.html
看下,或許有協助
------解決方案--------------------
解決這種問題最好的辦法就是截包,看看正常的包和curl通訊的包有什麼不同就知道了
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.