Sayalic
Sayalic

Reputation: 7650

Why my cURL in PHP doesn't follow redirects even I set "CURLOPT_RETURNTRANSFER" to true?

Code:

$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);

Result:

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>
"

Noticed I have set CURLPT_RETURNTRANSFER to true, it still gives me a 302 result. Could anyone please give me some suggestions?

Environment:

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.

Upvotes: 1

Views: 187

Answers (1)

Harikrishnan
Harikrishnan

Reputation: 9979

Set CURLOPT_FOLLOWLOCATION to make curl to follow redirects. You have set it to FALSE. Correct it with

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

CURLOPT_RETURNTRANSFER is to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.

Upvotes: 1

Related Questions