我正在尝试编写一个必须进行卷曲调用的小PHP代码,但它介于两者之间。请找到以下代码:
$url = 'XXXXXX';
$curlHandler = curl_init($url);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curlHandler, CURLOPT_ENCODING, '');
curl_setopt($curlHandler, CURLOPT_VERBOSE, TRUE);
print var_dump(curl_error($curlHandler))."\n";
print curl_exec($curlHandler);
curl_close($curlHandler);
我得到以下输出:
string(0) "" "* About to connect() to XXXXXX port 80 (#0)" "* Trying 72.52.8.197... * connected" "> GET XXXXXX HTTP/1.1" Host: XXXXXX Accept: */* Accept-Encoding: deflate, gzip"
这个php进程挂起后。
如果我按如下方式提出卷曲请求,它会起作用:
curl -v "XXXXXX"
* About to connect() to XXXXXX port 80 (#0)
* Trying 72.52.8.197... connected
> GET XXXXXX HTTP/1.1
> User-Agent: curl/7.22.0 (i686-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3
> Host: XXXXXX
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
< Content-Type: text/html; charset=UTF-8
< Date: Tue, 04 Mar 2014 11:02:15 GMT
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Location: XXXXXX
< Pragma: no-cache
< Server: Apache
< Set-Cookie: PHPSESSID=kkgmdajs0485tkjm2q7vrfl260; path=/; domain=.souq.com
< Set-Cookie: PLATEFORMC=sa; expires=Wed, 04-Mar-2015 11:02:15 GMT; path=/; domain=.souq.com
< Set-Cookie: PLATEFORML=ar; expires=Wed, 04-Mar-2015 11:02:15 GMT; path=/; domain=.souq.com
< Vary: Accept-Encoding
< Content-Length: 0
< Connection: keep-alive
< Set-Cookie: NSC_tpvr-83+63+9+208-91=ffffffff2d814a2945525d5f4f58455e445a4a423660;path=/;httponly
<
* Connection #0 to host XXXXXX left intact
* Closing connection #0
有人可以解释一下为什么php curl call和unix curl call有区别吗?
答案 0 :(得分:4)
命令行curl命令中包含未转义的&
,它们充当“make it background task”标记,[]
s之间的数字是bash为其分配的标识符。它们当然会立即退出,因为(例如)utm_campaign=desktop
不是真正的命令。您可以在bash手册的job control section中阅读更多内容。
只需将您的URL包装在命令行的"
中,这样curl命令就会收到整个字符串:
curl "http://...."
^ ^
如果您想查看详细消息(如php片段中所示),请在URL前添加-v
选项。
对于CURLOPT_FOLLOWLOCATION
,您需要-L
选项。
答案 1 :(得分:1)
命令行curl调用设置User-Agent,但PHP示例没有。
如果我对通过用户代理的URL尝试相同的请求,它可以正常工作。
尝试在PHP代码中添加一个,例如:
curl_setopt($curlHandler, CURLOPT_USERAGENT,
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Iron/31.0.1700.0 Chrome/31.0.1700.0 Safari/537.36');
如果您未指定用户代理或某些其他http标头(如accept-language或accept),则某些网站无法正常运行,此网站似乎是其中一个网站。