简体   繁体   English

Curl PHP如果传递参数则不会获取数据

[英]Curl PHP doesn't get the data if passed a parameters

I tried to access an API from a specified URL, but it doesn't work.我试图从指定的 URL 访问 API,但它不起作用。

here's my code:这是我的代码:

$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();

when I dump it, it shows a good URL.当我转储它时,它显示了一个很好的 URL。 I even tested it in apitester.com and it works.我什至在apitester.com中对其进行了测试,并且可以正常工作。 but when I run it via my web, it returns NULL .但是当我通过我的 web 运行它时,它返回NULL

Anything I miss here?我在这里想念什么吗?

test try this试试这个

 <?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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM