如何使用php

时间:2017-07-18 08:47:22

标签: arrays json curl php-curl

我需要卷曲GoGoVan API

的输入字符串
curl -X GET \
-H 'GoGoVan-API-Key: 3af4ba76-3767-4963-9680-327bb6d391d1' \
-H 'GoGoVan-User-Language: en-US' \
-F 'order[name]=John' \
-F 'order[phone_number]=61577364' \
-F 'order[pickup_time]=2016-01-20T18:00:00H' \
-F 'order[service_type]=delivery' \
-F 'order[vehicle]=motorcycle' \
-F 'order[title_prefix]=Corporate Order' \
-F 'order[extra_requirements][express_service]=true' \
-F 'order[extra_requirements][remark]=Please confirm the following 3 items' \
-F 'order[locations]=[[1.333948, 103.840142,"Toa Payoh, Singapore"],[1.353945, 103.843884,"5G Jalan Berjaya, Singapore"]]' \
'https://gogovan-staging-sg.herokuapp.com/api/v0/orders/price.json'

我使用以下代码创建查询

$query = urlencode(json_encode(array(
    'order' => array( 
        'name' => 'john', 
        'phone_number' => '61577364' ,
        'pickup_time' =>  '2017-07-18T18:00:00H',  
        'service_type' => 'delivery',  
        'vehicle' => 'motorcycle',  
        'title_prefix' => 'Corporate Order',  
        'extra_requirements' => array( 
            'express_service' => 'true', 
            'remark'=> 'Please confirm the following 3 items'
        ),
        'locations' => array(
            '1.333948, 103.840142,"Toa Payoh,  Singapore"',
            '1.353945, 103.843884,"5G Jalan Berjaya, Singapore"'
        )
    )
))));

$apifullurl = "https://gogovan-staging-sg.herokuapp.com/api/v0/orders/price.json";
$apifullurl = $apifullurl . '?json=' . $query;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apifullurl);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'GoGoVan-API-Key: 3af4ba76-3767-4963-9680-327bb6d391d1',
    'GoGoVan-User-Language: en-US'
)                                                                       
);     

$response = curl_exec($curl);
echo print_r($response);
$err_status = curl_error($curl);
curl_close($curl);

我得到的响应输出为null

{
    "breakdown": {
        "fee": {     
            "title": "Fee",
            "value":null
        }
    },
    "base": null,
    "total": 0,
    "payment_method": "cash"
}1

而应该有如下输出:

 {
   "base" : 10,
   "total" : 35,
   "breakdown" : {
      "fee" : {
         "title" : "Fee",
         "value" : 30
      },

      "extra_charge_for_express":{
        "title":"Express service",
        "value":3
        },

      "multi_point_concession":{
        "title":"Promotional Discount",
        "value":-3
        },
      },
   }
}

1 个答案:

答案 0 :(得分:0)

我也有同样的疑问。我认为跟随curl的查询生成有问题

order[locations] = [
    [
        1.333948, 
        103.840142, 
        "Toa Payoh, Singapore"
    ],
    [
        1.353945, 
        103.843884, 
        "5G Jalan Berjaya, Singapore"
    ]
]

这是一个数组数组。意味着多维数组。