读取 JSON 数据具有 PHP 的标记

Read JSON Data Have Token With PHP

如何使用 php 读取 JSON 数据响应?

这是代码:

$response = Unirest\Request::get("https://montanaflynn-spellcheck.p.mashape.com/check/?text=This+sentnce+has+some+probblems.",
  array(
    "X-Mashape-Key" => "MY X-Mashape-Key",
    "Accept" => "application/json"
  )
);

然后给我这个 json 数据 :

{
  "original": "This sentnce has some probblems.",
  "suggestion": "This sentence has some problems.",
   and ...
}

我想 return “suggestion” 在 url 和 $

这是一个例子:

$user_id = '123456789'
$mytext = '?' // I WANT RETURN "suggestion" HERE !

$url = 'https://api.telegram.org/mytoken/sendMessage?chat_id='.$user_id.'&text='.$mytext
file_get_contents($url);

使用json_decode将json_response转换为array.Add第二个参数'true',使它成为一个你应该熟悉的数组。

通过指定访问建议变量。

$response = Unirest\Request::get("https://montanaflynn-spellcheck.p.mashape.com/check/?text=This+sentnce+has+some+probblems.",
  array(
    "X-Mashape-Key" => "MY X-Mashape-Key",
    "Accept" => "application/json"
  )
);
$data=json_decode($response,true);
$url = 'https://api.telegram.org/mytoken/sendMessage?chat_id='.$user_id.'&text='.$data['suggestion'];
echo $url;

您可以使用 json_decode()。

见: http://php.net/manual/en/function.json-decode.php

您只需要做:

$response = json_decode($response,1);
echo $response['suggestion'];

将 json_decode() 的第二个参数设置为 1(真)将 return 一个 JSON 数据的关联数组。

如果您想使用您的代码示例将其包含在 URL 中:

$user_id = '123456789'
$json = json_decode($response,1);
$mytext = $json['suggestion'];

$url = 'https://api.telegram.org/mytoken/sendMessage?chat_id='.$user_id.'&text='.$mytext
file_get_contents($url);