Alex Andre
Alex Andre

Reputation: 9

cURL bad request error 400

I try to use cURL with PHP but I have an error 400 "bad request" Can you help me please ?

I try a lot of things but it is not working. I have modify the URL with false URL in this code.

I use soapUI.

Thanks, and sorry for my bad English

<?php

$participans_number = 1;
$soapUrl = "http://testservices.aireur.com/aireur-ws/Booking?wsdl";

$participans = '<book:participants>';
for($i = 0; $i<$participans_number; $i++){
    $participans = $participans . 
    '<book:Participant>
        <book:Civite> Mr </book:Civite>
        <book:Nom>SARE</book:Nom>
        <book:Prenom>Claude</book:Prenom>
        <book:DateNaissance>12/11/1990</book:DateNaissance>
        <book:Phone>0606060606</book:Phone>
        <book:Adresse1>Via Lo 25</book:Adresse1>
        <book:CodePostal>57025</book:CodePostal>
        <book:Ville>Piombino</book:Ville>
        <book:Pays>IT</book:Pays>
        <book:Email>[email protected]</book:Email>
    </book:Participant>';
}
$participans = $participans . '</book:participants>';

$xml_post_string ='<?xml version="1.0"?><soapenv:Envelope xmln:soapenv="http://schemas.xmlsaop.org/soap/envelope/" xmlns:book="bookingservices.aireur.com/">
    <soapenv:Header/>
    <soapenv:Body>
        <book:MakeBooking> 
            <!--Optional : --> 
            <book:agAccount>aireur9</book:agAccount>
            <book:agProd_1_ToI>aireur9</book:agProd_1_ToI>
            <!--Optional : --> 
            <book:agAgencyId>000000015139</book:agAgencyId>
            <!--Optional : --> 
            <book:agProd_1_RoomType>test</book:agProd_1_RoomType>
            <!--Optional : --> 
            <book:agProd_1_Code>test</book:agProd_1_Code>
            <!--Optional : --> 
            <book:trFromDate>12/11/2016</book:trFromDate>
            <!--Optional : --> 
            <book:trToDate>18/11/2016</book:trToDate>'.
            $participans . '
            <book:options></book:options>
            <book:reference>aireurIT1</book:reference>
            <!--Optional : --> 
        </book:MakeBooking>
    </soapenv:Body>
</soapenv:Envelope>';


$headers = array(
    "POST http://testservices.aireur.com/aireur-ws/Booking HTTP/1.1",
    "Host: http://bookingservices.aireur.com/",
    "SOAPAction: http://bookingservices.aireur.com/MakeBooking",
    "Content-Type: text/xml;charset=UTF-8",
    "Content-Lenght:". strlen($xml_post_string)
);

//Booking request - end

$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$reponse = curl_exec($ch);
curl_close($ch);

echo $reponse;

?>

Upvotes: 0

Views: 1322

Answers (2)

Flink
Flink

Reputation: 1006

For someone stumbling upon this from Google just like I did. Keep in mind:

$xml_post_string ='
<?xml version="1.0"?>

does not work, it needs to be on the first line:

$xml_post_string ='<?xml version="1.0"?>

This took me way too long to figure out.

Upvotes: 0

Daniel Stenberg
Daniel Stenberg

Reputation: 58002

  1. Remove this from the $headers array

"POST http://testservices.aireur.com/aireur-ws/Booking HTTP/1.1"

... as it isn't really a header and will make the request severely malformed.

  1. Don't set Host: yourself. It will only risk you do it wrong and curl will do it correctly itself based on the URL you use.

  2. As with Host, don't set Content-Length: and let curl set it itself based on the actual data you ask it to send.

Upvotes: 1

Related Questions