从url获取api json数据

时间:2018-12-20 10:02:06

标签: php json

请把我所有的这段代码输出一个json数据

echo json_encode(array(
                        "amount" => $user->amount,
                        "Sender" => $decoded->data->phone,
                        "Bank" => $user->bank,
                        "acct_number" => $user->acct,
                        "remarks" => $user->remarks,
                        "acct_name" => $user->receiver,

                    )

我很想从另一个网址获取它输出的数据,同时我的完整代码在下面,请帮我解决这个问题

<?php
header("Access-Control-Allow-Origin: http://localhost/kio/");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 60");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// files needed to connect to database
include_once '../config/database.php';
include_once '../objects/user.php';


// get database connection
$database = new Database();
$db = $database->getConnection();

// instantiate user object
$user = new User($db);

// check email existence here
// get posted data
$data = json_decode(file_get_contents("php://input"));

// set product property values
$token=isset($data->token) ? $data->token : "";
$user->id = $data->id;
$user->pin = $data->pin;
$user->bank = $data->bank;
$user->acct = $data->acct;
$user->remarks = $data->remarks;
$user->receiver = $data->receiver;

$transaction = $user->Transaction();

// files for jwt will be here
// generate json web token
include_once '../config/core.php';
include_once '../libs/php-jwt-master/src/BeforeValidException.php';
include_once '../libs/php-jwt-master/src/ExpiredException.php';
include_once '../libs/php-jwt-master/src/SignatureInvalidException.php';
include_once '../libs/php-jwt-master/src/JWT.php';
use \Firebase\JWT\JWT;



            if ($token) {

                try {
                    $decoded = JWT::decode($token, $key, array('HS256'));



                    try {

                if ($transaction && ($data->pin == $user->pin)) {



               // set response code and assign the user phone as user id


                http_response_code(200);

                echo json_encode(array(
                        "amount" => $user->amount,
                        "Sender" => $decoded->data->phone,
                        "Bank" => $user->bank,
                        "acct_number" => $user->acct,
                        "remarks" => $user->remarks,
                        "acct_name" => $user->receiver,

                    )
                );
            }

            else{

                http_response_code(400);

             // tell the user access denied  & show error message
             echo json_encode(
                array("Sever Response", 
                array
             ("message" => "Invalid Card Pin/ Card Used")
            ));
            }

            } catch (Exception $e) {

                // set response code
             http_response_code(407);

            // tell the user access denied  & show error message
            echo json_encode(array(
             "response" => "Access denied.",
             "error" => $e->getMessage()
    ));

            }

                }

                 catch (Exception $e) {

                      // set response code
    http_response_code(406);

    // tell the user access denied  & show error message
    echo json_encode(array(
        "response" => "Access denied.",
        "error" => $e->getMessage()
    ));

                }

            }

            else{

    // set response code
    http_response_code(363);

    // tell the user access denied
    echo json_encode(array("error" => "error"));
}

1 个答案:

答案 0 :(得分:0)

您可以通过使用

从API(网址)获取json响应
$json = file_get_contents($url);

该函数将URL作为参数。 $json将是一个json对象(manual)。

$obj = json_decode($json, true);

json_decode()会将json对象转换为数组。您可以操纵数组中的数据。