gpt4 book ai didi

php - 从 curl 响应编辑 json 响应

转载 作者:可可西里 更新时间:2023-11-01 13:19:36 25 4
gpt4 key购买 nike

<?php
$json_url = "http://openexchangerates.org/api/latest.json?app_id=xxxxx&callback=angular.callbacks._0";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $json_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$response = curl_exec($curl);

$jsonString = json_encode($response, true);
$data=json_decode($jsonString);

echo '<pre>',print_r($data),'</pre>';

$status = curl_getinfo($curl);
curl_close($curl);

输出是:

angular.callbacks._0({
"disclaimer": "xx",
"license": "xx",
"timestamp": 1368136869,
"base": "USD",
"rates": {
"AED": 3.672819,
"AFN": 53.209,
"ALL": 107.953875,
"AOA": 96.358934,
"ARS": 5.214887,
....
"XOF": 501.659003,
"XPF": 91.114876,
"ZMK": 5227.108333,
"ZMW": 5.314783,
"ZWL": 322.387247
}
})

但我需要将此输出编辑为这个输出(仅使用三种速率 (AED/AFN/AOA))。所以,基本上是在费率部分编辑 json 响应。我该怎么做?

angular.callbacks._0({
"disclaimer": "xx",
"license": "xx",
"timestamp": 1368136869,
"base": "USD",
"rates": {
"AED": 3.672819,
"AFN": 53.209,
"AOA": 107.953875,
}
})

最佳答案

$responsejson 格式不正确:

您必须从中删除不需要的部分:

$jsonString= substr($response, 21, -1);

你可以这样做:

<?php
$json_url = "http://openexchangerates.org/api/latest.json?app_id=5bf388eb6f7e40209b9418e6be44f04b&callback=angular.callbacks._0";

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $json_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$response = curl_exec($curl);

$jsonString= substr($response, 21, -1);
$data=json_decode($jsonString, true);

// Get the list of rates
$rates = $data['rates'];

$new_rates = array();
$new_rates['AED'] = $rates['AED'];
$new_rates['AFN'] = $rates['AFN'];
$new_rates['AOA'] = $rates['AOA'];

$data['rates'] = $new_rates;

echo 'angular.callbacks._0('.json_encode($data).')';

$status = curl_getinfo($curl);
curl_close($curl);

关于php - 从 curl 响应编辑 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16472170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com