需要帮助维护 PHP Curl 中的会话
我想从 http://www.ps3-themes.com/ 获取图像,但是每当我直接输入图片网址是不行的。它将其重定向到网站的主 URL。我正在使用以下代码。
$cookie_file = 'c:/tmp-cookie.txt';
$crl = curl_init();
curl_setopt($crl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($crl, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($crl, CURLOPT_URL, "http://www.ps3-themes.com/wp-content/uploads/2011/03/11414-FordF150RaptorSVT.jpg");
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1);
$html = curl_exec($crl);
if (file_exists($cookie_file)) {
print 'Created cookie file ' . $cookie_file;
} else {
print 'Could not create cookie file ' . $cookie_file;
}
我可以说这需要维持会话,但不知道如何做到这一点。-
I want to grab images from http://www.ps3-themes.com/, but whenever i directly enter image URL it doesn't work. It redirects it to main URL of site. I'm using following code.
$cookie_file = 'c:/tmp-cookie.txt';
$crl = curl_init();
curl_setopt($crl, CURLOPT_COOKIEFILE, $cookie_file);
curl_setopt($crl, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($crl, CURLOPT_URL, "http://www.ps3-themes.com/wp-content/uploads/2011/03/11414-FordF150RaptorSVT.jpg");
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1);
$html = curl_exec($crl);
if (file_exists($cookie_file)) {
print 'Created cookie file ' . $cookie_file;
} else {
print 'Could not create cookie file ' . $cookie_file;
}
I can say it requires maintaining session but not sure how to do that.-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从表面上看,这不是 cookie 问题,而是引荐来源网址问题——尝试从网站本身以外的任何地方显示图像将重定向到网站主页。这样做通常是为了防止热链接,这是一种带宽烦恼。
您可以通过将 cURL HTTP 引荐来源网址设置为适当的 URL 来解决此问题。
From the looks of it, this isn't a cookie issue but a referrer issue — attempting to display the image from anywhere but the site itself will redirect to the site home instead. This is usually done to prevent hotlinking, which is a bandwidth annoyance.
You can work around this by setting your cURL HTTP referrer to an appropriate URL.
在发出 CURL 请求之前,尝试使用 session_write_close() 。
Try using session_write_close() before you make the CURL request.
首先,您尝试直接在浏览器中打开图像。它打不开。可能是图像不存在或需要授权。
1st you try to open the image directly in browser. It does not open up. May be the image is not there or authorization is needed.