PHP - API URL在终端curl &浏览器中返回结果,但在Gulp / webpack / PHP dev env中返回301?

0pizxfdo  于 2022-12-08  发布在  Gulp
关注(0)|答案(1)|浏览(176)

这让我抓狂。我有一个PHP项目碰到了一个API,我在我的开发环境中不断得到一个301错误,这是Gulp / Webpack / PHP。URL直接在浏览器或ternimal cURL调用中返回结果很好。
你觉得我该去哪里找呢?

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // this url works in a browser
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // 301
xxhby3vn

xxhby3vn1#

您需要使用选项CURLOPT_FOLLOWLOCATION来遵循“301永久移动”

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

请参阅https://www.php.net/manual/en/function.curl-setopt.php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); // this url works in a browser
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$output = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

相关问题