首页 > 解决方案 > 如果传递参数,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

解决方案


试试这个

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


推荐阅读