代码:
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, "https://detail.1688.com/offer/543783250479.html?sk=consign");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$a = curl_exec($ch);
$data = curl_exec($ch);
curl_close($ch);
var_dump($data);
结果:
string(727) "HTTP/1.1 302 Moved Temporarily
Date: Tue, 16 May 2017 03:27:41 GMT
Content-Type: text/html
Content-Length: 266
Connection: keep-alive
Location: http://127.0.0.1/?sk=consign
Age: 0
X-Cache: HIT TCP_MEM_HIT dirn:0:151224522
Via: aserver011128044194.eu13[0,302-0,H]
url-hash: id=543783250479&detail.1688.com
Server: Tengine/Aserver
Strict-Transport-Security: max-age=31536000
Timing-Allow-Origin: *
EagleEye-TraceId: 0bfb814714949052613738714e8180
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html>
<head><title>302 Found</title></head>
<body bgcolor="white">
<h1>302 Found</h1>
<p>The requested resource resides temporarily under a different URI.</p>
<hr/>Powered by Tengine/Aserver</body>
</html>
"
注意到我将CURLPT_RETURNTRANSFER
设置为true,它仍然给出了302结果。有人可以给我一些建议吗?
环境:
php -v
PHP 7.0.8-2+deb.sury.org~xenial+1 (cli) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.8-2+deb.sury.org~xenial+1, Copyright (c) 1999-2016, by Zend Technologies
with blackfire v1.10.6, https://blackfire.io, by Blackfireio Inc.
答案 0 :(得分:1)
设置CURLOPT_FOLLOWLOCATION
以使curl遵循重定向。您已将其设置为FALSE
。用
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
CURLOPT_RETURNTRANSFER
是将转移作为curl_exec()返回值的字符串返回,而不是直接输出。