преобразование массива json в php из службы REST - PullRequest
1 голос
/ 06 ноября 2019

Я получал ответ от API REST flight в формате JSON, который я не смог преобразовать в массив из JSON.

Я пробовал кодирование json, но оно показывало только печать ответа json, ноне преобразует его в массив

Php Controller:

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);
    }

Ответы [ 2 ]

0 голосов
/ 07 ноября 2019

с помощью приведенного ниже кода (

    $payload = json_encode($jsonData);

    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

    curl_setopt($ch, CURLOPT_HTTPHEADER, 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, 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);)

) Теперь я смог преобразовать результат в массив

0 голосов
/ 06 ноября 2019

Вы не отправили этот запрос. Проверьте это: 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);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...