解析从结果的结果 - PHP
问题描述:
我怎样才能费的价值在此响应解析从结果的结果 - PHP
我整合邮递员API中的PHP应用程序,对于我使用的PHP卷曲电话,
这里是代码
<?php
class PostMates extends Controller {
public function getDeliveryQuote()
{
$url = "https://api.postmates.com/v1/customers/cus_XX/delivery_quotes";
$uname = "70538e7";
$pwd = "xxxx";
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded','Accept: application/json'));
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $uname . ":" . $pwd);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_POSTFIELDS, "dropoff_address=20 McAllister St, San Francisco, CA 94102&pickup_address=101 Market St, San Francisco, CA 94105");
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($process, CURLOPT_SSL_VERIFYPEER, false);
$return = curl_exec($process);
curl_close($process);
//var_dump($return);
print_r($return);
}
}
虽然我的print_r和的var_dump,下面给出的是响应。
我怎样才能得到从共振的费用金额。
我尝试像$ response-> fee和$ resonse ['fee'],但是我没有得到结果。
我怎样才能得到它。帮助请致电
HTTP/1.1 200 OK
Server: cloudflare-nginx
Date: Sun, 28 Feb 2016 18:26:14 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: __cfduid=d7e72569c2358ea8636fac1a1054815081456683973; expires=Mon, 27-Feb-17 18:26:13 GMT; path=/; domain=.postmates.com; HttpOnly
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
CF-RAY: 27be2d3566b42fd5-MAA
{"kind": "delivery_quote", "fee": 750, "created": "2016-02-28T18:26:14Z", "expires": "2016-02-28T18:31:14Z", "currency": "usd", "duration": 60, "dropoff_eta": "2016-02-28T19:31:14Z", "id": "dqt_KhBvlVTh8vQ8N-"}
答
在这里你去。你必须解析结果。
$result = json_decode($result,true); //decodes JSON to associative array
echo $result['fee']; //750
另外,如果你想使用一个对象格式
$result = json_decode($result); //decodes JSON to an object
echo $result->fee; //750
编辑
您还需要设置CURLOPT_HEADER
到false
这包括输出
响应头curl_setopt($process, CURLOPT_HEADER, false);
谢谢,但我alwasy越来越'试图获得非对象的属性' –
啊,所以这是实际的$请求内容相比,卷曲详细。输出改变的东西 - 检查答案的另一半答案的编辑 –