如何使php cURL区分大小写

问题描述

我正在尝试从Instagram链接获取数据作为JSON并获取一些对象,但是当我使用cURL时,我会得到一个空的JSON。

我相信问题是由cURL将url转换为小写字母引起的

https://www.instagram.com/p/CEwYF4FhXpz/?__a=1

不等同于

https://www.instagram.com/p/cewyf4fhxpz/?__a=1

所以我的问题是如何强制PHP CURL区分大小写

我的代码

$test_URL="https://www.instagram.com/p/CEwYF4FhXpz/?__a=1";

    $ch = curl_init();
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_URL,$test_URL);
    $result = curl_exec($ch);
    curl_close($ch);

    $obj = json_decode($result);

    return $obj;

解决方法

尝试添加。

curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
,

https://www.instagram.com/p/CEwYF4FhXpz/?__a=1

不等同于

https://www.instagram.com/p/cewyf4fhxpz/?__a=1

那部分是正确的...

我相信问题是由cURL将url转换为小写字母引起的

那是胡扯。这是您的代码正在发送的请求,但我已将https://www.instagram.com替换为http://localhost:9999,并在localhost端口9999上运行了一个netcat服务器:

$ nc -l 9999
GET /p/CEwYF4FhXpz/?__a=1 HTTP/1.1
Host: 127.0.0.1:9999
Accept: */*

您还可以使用CURLOPT_VERBOSE检查自己发送的网址,

$stderrh=tmpfile();
curl_setopt_array($ch,array(CURLOPT_VERBOSE=>1,CURLOPT_STDERR=>$stderrh));
$result = curl_exec($ch);
/* https://bugs.php.net/bug.php?id=76268 */
rewind($stderrh); 
$stderr=stream_get_contents($stderrh);
fclose($stderrh);
echo $stderr;

在此处运行代码时,也可以正常工作。因此,无论您是被IP阻止还是正在运行PHP的服务器都阻止了curl,请查看VERBOSE日志,您可能会得到一个提示