错误的http请求错误

时间:2017-07-21 12:06:11

标签: php php-5.6

我正在尝试动态调用API参数。以下是代码

   <?php
   $apikey="<API_KEY>";
   $senderid="TESTIN";
   $channel=2;
   $dcs=0; 
   $flashsms=0;
   $number="9171276664,678785612";
   $text="Hello Testing message dynamically";
   $route="11";

   $ch=curl_init("https://www.smsgatewayhub.com/api/mt/SendSMS?APIKey=".$apikey.'&senderid='.$senderid.'&channel='.$channel.'&DCS='.$dcs.'&flashsms='.$flashsms.'&number='.$number.'&text='.$text.'&route='.$route);
   ?>

抛出错误的http请求错误!!!

2 个答案:

答案 0 :(得分:2)

你需要对每个字符串进行urlencode,你也可以使用http构建查询使其更具可读性:如果你还有错误,你能发送错误吗?

<?php
   $params = Array(
      'apikey' => "xx",
      'senderid' => "TESTIN",
      'channel' => 2,
      'dcs' => 0,
      'flashsms' => 0,
      'number' => "1234,4567",
      'text' => "Hello Testing message dynamically",
      'route' => "11"
   ) ;

   $url = 'https://www.smsgatewayhub.com/api/mt/SendSMS?'.http_build_query($params) ;

   $ch=curl_init($url) ;
?>

答案 1 :(得分:1)

网址中不允许所有符号(例如:';','/','?',':','@','=','&amp;'和空格),所以你应该使用你的参数的urlencode函数。

<?php
...
$number=urlencode("xxxxxxxxxxxx,xxxxxxxxxxxx");
$text=urlencode("Hello Testing message dynamically");
...

$ch=curl_init("https://www.smsgatewayhub.com/api/mt/SendSMS?APIKey=".$apikey.'&senderid='.$senderid.'&channel='.$channel.'&DCS='.$dcs.'&flashsms='.$flashsms.'&number='.$number.'&text='.$text.'&route='.$route);
?>