dsi36131 2012-03-13 11:34
浏览 378
已采纳

CURL:尝试调用http url时出现BAD REQUESt错误

I am trying to call an sms api using a http url .I am trying to call the url using curl in php.I get a BAD REQUEST error .Please explain what I am doing wrong.

// create a new cURL resource
    $ch = curl_init();
    $string1 = "http://api.znisms.com/post/smsv3.asp?userid=alpesh67&apikey=74c6314840a16c5e7db00415a03181f7&message= Congratulation you have been successfully registered in the Placement Management System 
 Email:".$email."
 Password:".$password."&senderid=PMS12345&sendto=".$contactno."";
    echo $string1;
    // set URL and other appropriate options
     curl_setopt($ch, CURLOPT_URL, $string1);
    // grab URL and pass it to the browser
    curl_exec($ch);
    //close cURL resource, and free up system resources
    curl_close($ch);
    //SMS END

I get the following error:

http://api.znisms.com/post/smsv3.asp?userid=alpesh67&apikey=74c6314840a16c5e7db00415a03181f7&message= Congratulation you have been successfully registered in the Placement Management System Email:alpeshhi@gmail.com Password:123456789&senderid=PMS12345&sendto=9773396773
Bad Request
  • 写回答

4条回答 默认 最新

  • dougua3706 2012-03-13 11:42
    关注

    You can't use spaces in a URL. You need to url encode this string:

    &message= Congratulation you have been successfully registered in the Placement Management System 
     Email:".$email."
     Password:".$password."
    

    http://php.net/manual/en/function.urlencode.php

    Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the » RFC 3986 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs.

    I would do something to the effect of:

    // create a new cURL resource
        $ch = curl_init();
        $encoded_message = urlencode( "Congratulation you have been successfully registered in the Placement Management System 
     Email:".$email."
     Password:".$password)
        $string1 = "http://api.znisms.com/post/smsv3.asp?userid=alpesh67&apikey=74c6314840a16c5e7db00415a03181f7&message=".$encoded_message."&senderid=PMS12345&sendto=".$contactno."";
        echo $string1;
        // set URL and other appropriate options
         curl_setopt($ch, CURLOPT_URL, $string1);
        // grab URL and pass it to the browser
        curl_exec($ch);
        //close cURL resource, and free up system resources
        curl_close($ch);
        //SMS END
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么