如果传递了参数,则Curl PHP无法获取数据

时间:2019-10-27 04:54:09

标签: 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

我想念这里吗?

1 个答案:

答案 0 :(得分:0)

测试尝试

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