由于完整路径 Cookie 路径,您的代码无法正常工作,并确保它cookie.txt
是可写的
尝试
var_dump(getString("http://google.com"));
function getString($url) {
$ch = curl_init();
$cookie = __DIR__ . '/cookie.txt' ;
touch($cookie);
if(!is_writable($cookie))
{
die("Can't write to cookie");
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
cookie.txt 输出
# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.
.google.com TRUE / FALSE 1411737249 PREF ID=ff7979720d6a1237:FF=0:TM=1348665249:LM=1348665249:S=bRYSIBSW9Cd7PKOr
#HttpOnly_.google.com TRUE / FALSE 1364476449 NID 64=tcm3RUM8R_1ch9eD6tuFi4lObBjSNdxqwMHbpchYCQoUpghIjZbiNw8AdAm0buTAVF0SqUsZsYEs7PAWhJdhutO11EQ9y8iXwuQ9dsPmdWlt86BAa7hxRqQcjSoX9Bep
.google.com.ng TRUE / FALSE 1411737252 PREF ID=9428863ec2e741f5:FF=0:TM=1348665252:LM=1348665252:S=s7wtyWMM9OnRYoE4
#HttpOnly_.google.com.ng TRUE / FALSE 1364476452 NID 64=Gyszb-4_10nzvSU6kGzBj5UQRTnB7purbAH0reBytKi_pn9m3R-0BXGBEjrkmMBmOYfFpfIQOYLaCgi5LfKOIcnPCrTpTpV9LVld-Xf9pq7U7W5QaZ63a_yHIG9Vmcir