php - PHP:为 RESTful API 执行 cURL --location --request
问题描述
我无法从 PHP 脚本对 RESTful API 执行非常简单的请求。
以下:
curl --location --request GET 'https://api.thatrestfulsite.com/api/utilities/listassets?apiKey=0123456789'
从命令行或在 bash 脚本中调用时效果很好,但我很生气尝试从 PHP 脚本执行相同的 cURL 请求,即尝试使用 curl_setopt 转换相同的 cURL 请求。
我的意思是,下面的代码:
$url = 'https://api.thatrestfulsite.com/api/utilities/listassets?apiKey=0123456789'
$ch = curl_init();
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURL_HTTP_VERSION_2TLS, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
$http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
echo "HTTP STATUS: $http_status\nREQ OUTPUT: $output";
curl_close( $ch );
...失败,出现意外的 HTTP 301(永久移动)状态。
我是 curl_setopt 的新手(棘手的主题恕我直言),我不确定,但我想问题是,虽然我已经阅读了文档,但我无法找到正确翻译 '--location ' 操作说明。
你能告诉我我错过了什么吗?
先感谢您。
解决方案
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
推荐阅读
- javascript - 如何在 Javascript 中翻译单行条件
- sql - 更新 Oracle 目录视图和表
- c# - 列表框绑定 (Caliburn.Micro) - 疑难解答
- python - 派生自 collections.Counter 的类在腌制时会丢失值
- python-3.x - 在数据框上保存已删除的 html 标签
- r - 功率回归中stat_smooth和lm(使用log)之间的R差异?
- python - Django ORM:从大数据集中每天检索第一个测量值
- php - 选择一天并使用第二个参数的函数你可以在 PHP 中添加或反转日期吗?
- r - R 中创建新行并附加到数据框的一种简洁方式
- python - 如何在python中将字符串从一个txt文件替换为另一个txt文件