php - 如果传递参数,Curl PHP 不会获取数据
问题描述
我试图从指定的 URL 访问 API,但它不起作用。
这是我的代码:
$query = http_build_query([
'origin' => '153',
'destination' => (int)$_GET['city'],
'weight' => (int)$_GET['weight'],
'courier' => 'jne'
//i even tried to pass method: get but it doesn't work
]);
$url = 'https://astrajingga.com/PROJECT/LPPIConnect/API/getDelivery.php' . $query;
$newCurl = curl_init($url);
// hidden, not the real authorization
$customHeaders = array(
'Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxx'
);
curl_setopt($newCurl, CURLOPT_HTTPHEADER, $customHeaders);
curl_setopt($newCurl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($newCurl, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($newCurl);
$myData = json_decode($result, true);
var_dump($myData);
die();
当我转储它时,它显示了一个很好的 URL。我什至对它进行了测试,apitester.com
它可以工作。但是当我通过我的网络运行它时,它会返回NULL
.
我在这里想念什么吗?
解决方案
试试这个
<?php
$query = array(
'origin' => '153',
'destination' => 120,
'weight' => 56,
'courier' => 'jne'
//i even tried to pass method: get but it doesn't work
);**strong text**
// $url = 'https://astrajingga.com/PROJECT/LPPIConnect/API/getDelivery.php' . $query;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://astrajingga.com/PROJECT/LPPIConnect/API/getDelivery.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
$query);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
print_r($server_output);
die();
推荐阅读
- angular - Angular:自定义 FormControl 在 FormGroup.reset() 之后不更新视图
- modal-dialog - Symfony 4:以模态显示动态值
- azure - Azure appservice 添加 webjob 失败
- c# - 控制台应用程序后台服务中 httpclientfactory 的 DI
- python-3.x - 如何检查列表值是否存在于也是列表的列值中
- c++ - 将 unique_ptr 设置为前向声明类型的解决方法
- excel - 在 VBA 中清除另一个工作表中的过滤器后,将数据复制并粘贴到新工作表中
- react-native - 使用 expo 弹出应用程序后的问题
- php - 将 cURL 请求转换为 PHP
- saml-2.0 - spring-dsl 的自定义 `AuthenticationSuccessHandler`