gpt4 book ai didi

php - 为什么来自 PHP 文件的 cURL 请求不起作用,而来自 Linux 控制台的相同 cURL 请求却起作用?

转载 作者:行者123 更新时间:2023-12-02 21:31:38 25 4
gpt4 key购买 nike

我正在尝试编写必须进行curl 调用的小型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: XXXXXXAccept: */*Accept-Encoding: deflate, gzip"

After this php process hangs.

While if I make curl request as follows, it works:

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 调用和 unix curl 调用有区别吗?

最佳答案

命令行curl命令中有未转义的&,它们充当“使其成为后台任务”标记,[]之间的数字是bash 为它们分配的标识符。它们当然会立即退出,因为(例如)utm_campaign=desktop 不是真正的命令。您可以在 job control section 中阅读更多内容bash 手册。

只需在命令行上将 URL 括在 " 中,这样curl 命令就会接收整个字符串:

curl "http://...."
^ ^

如果您想查看详细消息(如 php 代码片段中所示),请在 URL 前添加 -v 选项。
对于CURLOPT_FOLLOWLOCATION,您将需要-L选项。

关于php - 为什么来自 PHP 文件的 cURL 请求不起作用,而来自 Linux 控制台的相同 cURL 请求却起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22168591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com