简体   繁体   English

cURL错误的请求错误400

[英]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 ? 我尝试将cURL与PHP结合使用,但出现错误400“错误请求”,请问您能帮我吗?

I try a lot of things but it is not working. 我尝试了很多事情,但是没有用。 I have modify the URL with false URL in this code. 我在此代码中用错误的URL修改了URL。

I use soapUI. 我使用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>test@test.fr</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;

?>
  1. Remove this from the $headers array 从$ headers数组中删除它

"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. 不要设置Host:自己。 It will only risk you do it wrong and curl will do it correctly itself based on the URL you use. 它只会冒您做错事情的风险,而curl会根据您使用的URL本身正确地做到这一点。

  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. 与Host一样,不要设置Content-Length: :,而是让curl根据您要求它发送的实际数据自行设置它。

For someone stumbling upon this from Google just like I did. 对于像我一样在Google上绊脚石的人。 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. 我花了很长时间才弄清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM