我使用Hertz Api預訂車輛查找服務來獲得與所提供的行程訊息車輛列表。這種特定服務的文檔都可以找到here如何將JSON發佈到API端點?
注意:他們需要你有一個訪問令牌來訪問他們的開發文檔,但你可以很容易地只填寫表單得到一個。或者,你可以使用我在下面的()。
下面是我用
$action_uri = 'https://www.hertz.co.nz/rentacar/rest/home/form';
$post_fields = array(
'accessToken' => '1616634772671643728-8915933508415978751',
'ageSelector' => '21',
'continueButton' => 'continue',
'dropoffDay' => '6/6/2013',
'dropoffTime' => '9:00',
'pickupDay' => '5/6/2013',
'pickupHiddenEOAG' => 'AKLT50',
'pickupLocation' => 'Auckland Airport',
'pickupTime' => '09:00'
);
$data_string = json_encode($post_fields);
$ch = curl_init($action_uri);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/Cookiejar.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/Cookiejar.txt');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));
$content = curl_exec($ch);
$headers = curl_getinfo($ch);
$error_number = curl_errno($ch);
$error_message = curl_error($ch);
curl_close($ch);
print_r($content);
var_dump($headers);
var_dump($error_number);
var_dump($error_message);
根據他們的文檔的代碼,我需要JSON格式提交POST參數。因此,我將CURLOPT_HTTPHEADER設置爲您在上面看到的內容。
我的問題是,我從$content
越來越亂碼數據這樣
‹Eͱ‚0৸t щÕÉÅã`B8¤±´M[ ðº]¿Þýÿƒ¶î"Er˜½üÌ¿Iø ‡M{#µÎi›S:ŽcÒ¢qSR©DNÔ t¬bÆÍÀW’¶ªÃïý–zV5ú˜Sšz]ÂéÐZöÂÿÖK±Š·¯Þq¯áÁ;„´@f|'`00ÁkÐûŽ;Åõþ̲cIÖ„2´EË¥Åpó
這是$headers
array
'url' => string 'https://www.hertz.co.nz/rentacar/rest/home/form' (length=47)
'content_type' => string 'application/json' (length=16)
'http_code' => int 400
'header_size' => int 479
'request_size' => int 694
'filetime' => int -1
'ssl_verify_result' => int 20
'redirect_count' => int 0
'total_time' => float 1.078
'namelookup_time' => float 0
'connect_time' => float 0.25
'pretransfer_time' => float 0.75
'size_upload' => float 261
'size_download' => float 187
'speed_download' => float 173
'speed_upload' => float 242
'download_content_length' => float -1
'upload_content_length' => float 0
'starttransfer_time' => float 1.078
'redirect_time' => float 0
'certinfo' =>
array
empty
輸出正如你看到的,我HTTP_CODE 400這是一個壞請求。
任何想法,爲什麼這是?我是否將一個JSON變量發佈到API權限?
燁不是赫茲的文件,它的工作。想知道爲什麼這些信息不在他們的文檔中。謝謝! – arvinsim