我們真的需要看到更多的代碼,但這裏有一個示例位,它從初始請求中收集會話cookie,然後在後續POST中使用它。它使用匿名代理在任意URL上運行GET請求,希望它可以幫助你(雖然它不使用COOKIEJAR,但我認爲它可能仍然有幫助)。
<?php
define('TARGET_URL', 'http://moxune.com');
echo 'Sending initial request' . PHP_EOL;
$aHeaders = get_headers("http://420proxy.info");
foreach($aHeaders as $sHeader) {
if(stripos($sHeader, 'set-cookie') !== false) {
// extract the cookie from the first response
$aCookie = explode(':', $sHeader);
$sCookie = trim(array_pop($aCookie));
$oCookie = http_parse_cookie($sCookie);
echo 'Cookie extracted, trying to POST now' . PHP_EOL;
// OK, now let's try the POST request
$ch = curl_init('http://420proxy.info/includes/process.php?action=update');
curl_setopt($ch, CURLOPT_REFERER, '420.proxy.info');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_COOKIE, $sCookie);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: 100-continue'));
//curl_setopt($ch, CURLOPT_COOKIE, http_build_cookie((array)$oCookie));
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'u' => TARGET_URL,
'allowCookies' => 'off',
'encodeURL' => 'off',
'stripJS' => 'on'
)
);
$response = curl_exec($ch);
die(var_dump($response));
}
}
'CURLOPT_COOKIEJAR零件確實創建了一個cookie' - 您是否確認創建的文件實際上具有cookie值並且不是空的?你能否從第一個請求的響應頭部確認「Set-Cookie」頭部存在? – drew010 2012-02-13 23:49:15
檢查,cookie被創建並且內容反映頭的值。 – Arnoldiusss 2012-02-13 23:52:11
我有點茫然,那麼你是否願意發佈更完整的代碼,也許你試圖訪問的網站的URL,所以我可以試試? – drew010 2012-02-14 00:12:09