从没有来自cURL的头的json响应中获取值

时间:2017-03-22 13:06:45

标签: php json curl

我想从执行cUrl post请求后得到的响应中获取json数组中“token”的值。在print_r($response);之后它打印出所有这些。但我只想要json字符串和php的一个特定值。

以下是我的回复。在回复之前我不希望这一切

HTTP/1.1 200 OK<br/>
Content-Type: application/json<br/>
Content-Length: 312<br/>
Connection: keep-alive<br/>
Vary: Accept-Encoding<br/>
Status: 200 OK<br/>
Cache-Control: max-age=0, private, must-revalidate<br/>
Date: Wed, 22 Mar 2017 12:52:25 GMT<br/>
Strict-Transport-Security: max-age=31536000<br/>
X-Request-Id: 9418df03bea4e4884522b703d0eec504<br/>
X-UA-Compatible: IE=Edge,chrome=1<br/>
ETag: "dd4de4d3a6e4e499d6a034ce784d2d76"<br/>
X-Runtime: 0.633173<br/>
X-Content-Type-Options: nosniff<br/>
X-Rack-Cache: invalidate, pass<br/>
X-Powered-By: Phusion Passenger 5.0.28<br/>
Server: nginx/1.10.0 + Phusion Passenger 5.0.28

{"response_code":"00","response_text":"Mobile wallet payment request has been issued.","description":"You will receive a bill prompt shortly on your number 0546653444 with invoice no. 201562656, kindly complete it.","transaction_id":"DTV408402","token":"8268dfffa46a16b0665a76","mobile_invoice_no":"201562656"}

3 个答案:

答案 0 :(得分:0)

您需要告诉cURL您不需要标题:

curl_setopt($ch, CURLOPT_HEADER, 0);

答案 1 :(得分:-2)

你可以在这个响应上使用php的explode()函数。或者,您可以调整curl请求,以便不根据您的请求返回标题。

$split=explode("\n\n",$response);
$array=json_decode($split[1],true);
var_dump($array);

如果这不起作用,修改explode功能以拆分“\ r \ n \ r \ n”而不是“\ n \ n”;

答案 2 :(得分:-2)

您可以使用JSON解码来完成。

$json = $Your_respone_variable;
$obj = json_decode($json);
print $obj->{'token'}; // token can be replaced by any other key of your JSON

有关更多信息,请查看: http://php.net/manual/de/function.json-decode.php