我想从PHP脚本返回JSON。
我只是重复结果吗?我必须设置内容类型头吗?
我想从PHP脚本返回JSON。
我只是重复结果吗?我必须设置内容类型头吗?
当前回答
虽然你通常没有它也没问题,但你可以也应该设置Content-Type头文件:
<?php
$data = /** whatever you're serializing **/;
header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);
如果我没有使用特定的框架,我通常允许一些请求参数修改输出行为。通常对于快速故障排除,不发送报头或有时print_r数据有效负载来观察它(尽管在大多数情况下,这应该是不必要的)是很有用的。
其他回答
如果你需要从发送自定义信息的php中获取json,你可以添加这个头('Content-Type: application/json');在打印任何其他东西之前,所以然后你可以打印你的自定义echo '{"monto": "'.$monto[0]->valor.'","moneda":"'.$moneda[0]->nombre.'","simbolo":"'.$moneda[0]->simbolo.'"}';
无论何时你试图为API返回JSON响应,或者确保你有适当的标题,也确保你返回一个有效的JSON数据。
下面是示例脚本,它可以帮助您从PHP数组或返回JSON响应 来自JSON文件。
PHP脚本(代码):
<?php
// Set required headers
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
/**
* Example: First
*
* Get JSON data from JSON file and retun as JSON response
*/
// Get JSON data from JSON file
$json = file_get_contents('response.json');
// Output, response
echo $json;
/** =. =.=. =.=. =.=. =.=. =.=. =.=. =.=. =.=. =.=. =. */
/**
* Example: Second
*
* Build JSON data from PHP array and retun as JSON response
*/
// Or build JSON data from array (PHP)
$json_var = [
'hashtag' => 'HealthMatters',
'id' => '072b3d65-9168-49fd-a1c1-a4700fc017e0',
'sentiment' => [
'negative' => 44,
'positive' => 56,
],
'total' => '3400',
'users' => [
[
'profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg',
'screen_name' => 'rayalrumbel',
'text' => 'Tweet (A), #HealthMatters because life is cool :) We love this life and want to spend more.',
'timestamp' => '{{$timestamp}}',
],
[
'profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg',
'screen_name' => 'mikedingdong',
'text' => 'Tweet (B), #HealthMatters because life is cool :) We love this life and want to spend more.',
'timestamp' => '{{$timestamp}}',
],
[
'profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg',
'screen_name' => 'ScottMili',
'text' => 'Tweet (C), #HealthMatters because life is cool :) We love this life and want to spend more.',
'timestamp' => '{{$timestamp}}',
],
[
'profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg',
'screen_name' => 'yogibawa',
'text' => 'Tweet (D), #HealthMatters because life is cool :) We love this life and want to spend more.',
'timestamp' => '{{$timestamp}}',
],
],
];
// Output, response
echo json_encode($json_var);
JSON文件(JSON数据):
{
"hashtag": "HealthMatters",
"id": "072b3d65-9168-49fd-a1c1-a4700fc017e0",
"sentiment": {
"negative": 44,
"positive": 56
},
"total": "3400",
"users": [
{
"profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg",
"screen_name": "rayalrumbel",
"text": "Tweet (A), #HealthMatters because life is cool :) We love this life and want to spend more.",
"timestamp": "{{$timestamp}}"
},
{
"profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg",
"screen_name": "mikedingdong",
"text": "Tweet (B), #HealthMatters because life is cool :) We love this life and want to spend more.",
"timestamp": "{{$timestamp}}"
},
{
"profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg",
"screen_name": "ScottMili",
"text": "Tweet (C), #HealthMatters because life is cool :) We love this life and want to spend more.",
"timestamp": "{{$timestamp}}"
},
{
"profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg",
"screen_name": "yogibawa",
"text": "Tweet (D), #HealthMatters because life is cool :) We love this life and want to spend more.",
"timestamp": "{{$timestamp}}"
}
]
}
JSON Screeshot:
您可以使用这个小型PHP库。它发送头文件并给你一个容易使用它的对象。
它看起来是这样的:
<?php
// Include the json class
include('includes/json.php');
// Then create the PHP-Json Object to suits your needs
// Set a variable ; var name = {}
$Json = new json('var', 'name');
// Fire a callback ; callback({});
$Json = new json('callback', 'name');
// Just send a raw JSON ; {}
$Json = new json();
// Build data
$object = new stdClass();
$object->test = 'OK';
$arraytest = array('1','2','3');
$jsonOnly = '{"Hello" : "darling"}';
// Add some content
$Json->add('width', '565px');
$Json->add('You are logged IN');
$Json->add('An_Object', $object);
$Json->add("An_Array",$arraytest);
$Json->add("A_Json",$jsonOnly);
// Finally, send the JSON.
$Json->send();
?>
尝试json_encode对数据进行编码,并设置带有header的content-type (' content-type: application/json');
如果你在WordPress中这样做,那么有一个简单的解决方案:
add_action( 'parse_request', function ($wp) {
$data = /* Your data to serialise. */
wp_send_json_success($data); /* Returns the data with a success flag. */
exit(); /* Prevents more response from the server. */
})
请注意,这不在wp_head钩子中,该钩子将始终返回大部分头部,即使您立即退出。parse_request在序列中出现得更早。