从REST服务将json转换为php数组

我从REST Flight API中获得了JSON格式的响应,但无法从JSON转换为数组。

我已经尝试过json编码,但是它只显示打印json响应,而没有将其转换为数组

Php控制器:

public function search_flites() {php controller

        header('Content-type: application/json');
        $this->load->library('curl');
        $result = $this->curl->simple_get('http://13.235.39.41:8080/ettafly/api/session');
        $Data = json_decode($result);

        $session_id = $Data->SessionId;
        $url = 'http://13.235.39.41:8080/ettafly/api/flightavaliblity';
        $ch = curl_init($url);

        $jsonData = array(
            "user_id" => "Ettafly_APITest2019","user_password" => "Ettafly_TestPswd2019","access" => "Test","ip_address" => "13.235.39.41","session_id" => "$session_id","journey_type" => "OneWay","airport_from_code" => "DEL","airport_to_code" => "BOM","departure_date" => "2019-11-16","return_date" => "2019-11-18","adult_flight" => "1","child_flight" => "0","infant_flight" => "0","class_type" => "Economy","target" => "Test"
        );

        $ch = curl_init($url);
        $jsonDataEncoded = json_encode($jsonData,JSON_PRETTY_PRINT);

        curl_setopt($ch,CURLOPT_POST,1);

        curl_setopt($ch,CURLOPT_POSTFIELDS,$jsonDataEncoded);

        $result2 = curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/json'));
        $Data2 = json_decode($result2,true);
    }
l0123b0123 回答:从REST服务将json转换为php数组

您没有发送该请求。 检查一下:https://www.php.net/manual/en/curl.examples.php#88055

<?php

public function search_flites() {

    header('Content-type: application/json');
    $this->load->library('curl');
    $result = $this->curl->simple_get('http://13.235.39.41:8080/ettafly/api/session');
    $Data = json_decode($result);

    $session_id = $Data->SessionId;
    $url = 'http://13.235.39.41:8080/ettafly/api/flightavaliblity';
    $ch = curl_init($url);

    $jsonData = array(
        "user_id" => "Ettafly_APITest2019","user_password" => "Ettafly_TestPswd2019","access" => "Test","ip_address" => "13.235.39.41","session_id" => "$session_id","journey_type" => "OneWay","airport_from_code" => "DEL","airport_to_code" => "BOM","departure_date" => "2019-11-16","return_date" => "2019-11-18","adult_flight" => "1","child_flight" => "0","infant_flight" => "0","class_type" => "Economy","target" => "Test"
    );

    $ch = curl_init($url);
    $jsonDataEncoded = json_encode($jsonData,JSON_PRETTY_PRINT);

    curl_setopt($ch,CURLOPT_POST,1);

    curl_setopt($ch,CURLOPT_POSTFIELDS,$jsonDataEncoded);

    $curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/json'));

    // You have to exec the curl request to get a response.
    $result2 = curl_exec($ch);
    $Data2 = json_decode($result2,true);

    var_dump($Data2,JSON_PRETTY_PRINT);
}
,

通过使用以下代码       (

    $payload = json_encode($jsonData);

    curl_setopt($ch,$payload);

    curl_setopt($ch,array('Content-Type:application/json'));

    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

    $result = curl_exec($ch);

    $jsonData2 = json_decode($result,true);)

而不是 (

$jsonDataEncoded = json_encode($jsonData,JSON_PRETTY_PRINT);

curl_setopt($ch,1);

curl_setopt($ch,$jsonDataEncoded);

$curl_setopt($ch,array('Content-Type: application/json'));

// You have to exec the curl request to get a response.
$result2 = curl_exec($ch);
$Data2 = json_decode($result2,true);

var_dump($Data2,JSON_PRETTY_PRINT);)

现在我能够将结果转换为数组

本文链接:https://www.f2er.com/3150510.html

大家都在问