0

我正在使用Hertz Api Reservation 车辆查找服务来获取包含所提供行程信息的车辆列表。可以在此处找到此特定服务的文档

注意:他们要求您拥有访问令牌才能访问他们的开发人员文档,但您只需填写表格即可轻松获得。或者,您可以只使用我在下面的那个()。

这是我使用的代码

$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 吗?

4

2 回答 2

1

这是压缩的答案。添加CURLOPT_ENCODING

curl_setopt($ch, CURLOPT_ENCODING, '');
于 2013-06-06T09:54:02.753 回答
0

我认为这些现在是旧链接,因为我无法在这些链接上获取文档

于 2015-12-02T06:40:48.310 回答