使用curl发布数据,以便在现场预订expedia

时间:2015-07-01 11:29:17

标签: php wordpress api curl

我通过一个api发布一个请求进行现场预订。 当我通过一个表单操作发布数据并提交时,我将得到响应。 但是当我试图通过curl发布相同的url来获得响应时,我没有得到任何数据。

网址是: -

https://book.api.ean.com/ean-services/rs/hotel/v3/res?
minorRev=99
&cid=55505
&sig=1893d9f7e3e9fbd3f8a36f43cd61287d
&apiKey=1bn8n4or4tjajq23fe4l6m18lp
&customerUserAgent=Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0
&customerIpAddress=223.30.152.118
&customerSessionId=e80df6de9008af772cfb48a389465415
&locale=en_US
&currencyCode=USD
&xml=<HotelRoomReservationRequest>
<hotelId>106347</hotelId>
<arrivalDate>10/30/2015</arrivalDate>
<departureDate>11/1/2015</departureDate>
<supplierType>E</supplierType>
<rateKey>469e1aff-49de-4944-a64d-25d96ccde3aa</rateKey>
<rateCode>200706716</rateCode>
<roomTypeCode>200127420</roomTypeCode>
<chargeableRate>252.00</chargeableRate>
<RoomGroup>
    <Room>
        <numberOfAdults>2</numberOfAdults>
        <numberOfChildren>2</numberOfChildren>
        <childAges>3,7</childAges>
        <firstName>test</firstName>
        <lastName>test</lastName>
        <smokingPreference>NS</smokingPreference>
        <bedTypeId>23</bedTypeId>
    </Room>
</RoomGroup>
<ReservationInfo>
    <email>test@travelnow.com</email>
    <firstName>test</firstName>
    <lastName>test</lastName>
    <homePhone>2145370159</homePhone>
    <workPhone>2145370159</workPhone>
    <creditCardType>CA</creditCardType>
    <creditCardNumber>5401999999999999</creditCardNumber>
    <creditCardExpirationMonth>04</creditCardExpirationMonth>
    <creditCardExpirationYear>2017</creditCardExpirationYear>
    <creditCardIdentifier>123</creditCardIdentifier>
</ReservationInfo>
<AddressInfo>
    <address1>travelnow</address1>
    <city>Seattle</city>
    <stateProvinceCode>WA</stateProvinceCode>
    <countryCode>US</countryCode>
    <postalCode>98117</postalCode>
</AddressInfo>
</HotelRoomReservationRequest>

我正在使用该代码

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
$header[] = "Accept: application/json";
        $header[] = "Accept-Encoding: gzip";
        $header[] = "Content-length: 0";


        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_VERBOSE, true);
        $verbose = fopen('php://temp', 'rw+');
        curl_setopt($ch, CURLOPT_STDERR, $verbose);

        $result = curl_exec($ch);


echo '<pre>';
print_r($result);

请帮助我如何通过curl发布这些数据以获得响应。我在wordpress中使用它来进行付款预订。

1 个答案:

答案 0 :(得分:1)

总结我发布的评论:

  • 如果您尝试将数据发送到curl网址,则需要使用 CURLOPT_POSTFIELDS
  • 对于标题Content-length,0值可以 是一个问题
  • 查看您是否实际连接到URL(for 例如,您需要传递用户名,密码吗? 身份验证)您可能希望回显出返回的标头 CURLOPT_HEADER
  • 您的网址似乎是带有名称/值对的GET请求,但您将curl称为POST
  • 一个非常好的参考是在PHP.net http://php.net/manual/en/function.curl-setopt.php