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条)

报告相同问题?

悬赏问题

  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决
  • ¥15 关于华为5g模块mh5000-31接线问题
  • ¥15 keil L6007U报错
  • ¥15 webapi 发布到iis后无法访问