HTTP错误400.请求格式错误。在curl请求

时间:2017-04-19 08:45:16

标签: php curl laravel-5.2

我正在使用curl请求来获取短信报告但面临一些问题。我也通过编码网址但仍然是相同的问题。 正在显示400个错误请求。

$url="http://api.smscountry.com/smscwebservices_bulk_reports.aspx?user=&passwd=&fromdate=19/04/2017 00:00:00&todate=19/04/2017 23:59:59&jobno=60210892";  //callbackURL=http://www.jouple.com/marketing/public/save/sms
$ch = curl_init();
if (!$ch){
    die("Couldn't initialize a cURL handle");
}
$ret = curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// curl_setopt ($ch, CURLOPT_POSTFIELDS,
// "User=$user&passwd=$password&sid=$senderid");
// $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//If you are behind proxy then please uncomment below line and provide your proxy ip with port.
// $ret = curl_setopt($ch, CURLOPT_PROXY, "PROXY IP ADDRESS:PORT");
$curlresponse = curl_exec($ch); // execute

// print_r($ch);die;
if(curl_errno($ch))
    echo 'curl error : '. curl_error($ch);
if (empty($ret)) {
// some kind of an error happened
    die(curl_error($ch));
    curl_close($ch); // close cURL handler
} else {
    $info = curl_getinfo($ch);
    curl_close($ch); // close cURL handler

}

2 个答案:

答案 0 :(得分:0)

当您在URL中有空格时会发生这种情况。你必须逃脱网址。请遵循以下代码

<?php
$url="http://api.smscountry.com/smscwebservices_bulk_reports.aspx/";  //callbackURL=http://www.jouple.com/marketing/public/save/sms
$url2= "?user=&passwd=&fromdate=19/04/2017 00:00:00&todate=19/04/2017 23:59:59&jobno=60210892";
        $ch = curl_init();
        $url = $url . curl_escape($ch, $url2);
        if (!$ch){
            die("Couldn't initialize a cURL handle");
        }
        $ret = curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt ($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        // curl_setopt ($ch, CURLOPT_POSTFIELDS,
        // "User=$user&passwd=$password&sid=$senderid");
        // $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        //If you are behind proxy then please uncomment below line and provide your proxy ip with port.
        // $ret = curl_setopt($ch, CURLOPT_PROXY, "PROXY IP ADDRESS:PORT");
        $curlresponse = curl_exec($ch); // execute

        // print_r($ch);die;
        if(curl_errno($ch))
            echo 'curl error : '. curl_error($ch);
        if (empty($ret)) {
        // some kind of an error happened
            die(curl_error($ch));
            curl_close($ch); // close cURL handler
        } else {
            $info = curl_getinfo($ch);
            curl_close($ch); // close cURL handler

        }

在此之后,您将面临无效内容长度错误。取消注释CURLOPT_POSTFIELDS行并传递正确的凭据。肯定会起作用。

答案 1 :(得分:0)

您可以将 urlencode() 添加到您的参数中,例如 msg 和 mobile,例如:

    $sms = "Dear User, Your OTP for trip is $otp CSPL"; 
    $sms = urlencode($sms);
    $mobile =  urlencode($mobile);
    $url = "yoururl";
    $ch  = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    $responseJson = curl_exec($ch);
    $httpcode     = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);