2012-03-13 6 views
0

http urlを使用してSMS APIを呼び出そうとしています。curlを使用してURLを呼び出そうとしています。間違っている。CURL:http urlを呼び出そうとしたときに不正な要求が発生しました

// 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 \n Email:".$email."\n 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 

私は次のエラーを取得する:

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 protected] Password:123456789&senderid=PMS12345&sendto=9773396773 
Bad Request 

答えて

9

あなたがURLにスペースを使用することはできません。あなたは、エンコードにこの文字列をURLする必要があります。

&message= Congratulation you have been successfully registered in the Placement Management System \n Email:".$email."\n 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.

私はの効果に何かをするだろう:

// create a new cURL resource 
    $ch = curl_init(); 
    $encoded_message = urlencode("Congratulation you have been successfully registered in the Placement Management System \n Email:".$email."\n 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 
+0

私は、Webブラウザから作成したURLを呼び出すだけで、メッセージのスペースは問題ではありません。次のようにカールヘイダーを含めることをお勧めします。しかし、私はまだあなたのソリューションを試して、私はまだエラーが発生します。 – Jonah

+0

更新されたエラーがありますか? urlencode()を追加すると、エラーのURLの内容が異なるはずです。 – Drahkar

+0

urlencode関数は私のためのトリックでした。ありがとうございます – noobcode

1

それはスペースで渡されたメッセージが原因である可能性がありURLに urlencodを試してみてください

1

URLには空白があるので、urlencode()を使用してください。

$headers = array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.8) Gecko/20061025 Firefox/1.5.0.8"); 

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
関連する問題