网址编码错误?

时间:2013-07-28 10:35:43

标签: php api curl

http://techmentry.com/free.php上的我的PHP代码(free.php)是

<?php
{
//Variables to POST
$access_token = "b34480a685e7d638d9ee3e53cXXXXX";
$message = "hi";
$send_to = "existing_contacts";

//Initialize CURL data to send via POST to the API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://freesmsgateway.com/api_send");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
          array('access_token' => $access_token,
                'message' => urlencode('hi'),
                'send_to' => $send_to,)
          );

//Execute CURL command and return into variable $result
$result = curl_exec($ch);

//Do stuff
echo "$result";
}
?>

我收到了这个错误:消息是空白的

此错误表示:“消息字段为空或未正确进行URL编码”(由我的SMS网关告知)。但是你可以看到我的消息字段不是空白的。

2 个答案:

答案 0 :(得分:0)

我相信您无法将数组发送到CURLOPT_POSTFIELDS,您需要用以下内容替换该行

curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=".$accesstoken."&message=".urlencode('hi')."&send_to=".$send_to);

我希望这能解决它

答案 1 :(得分:0)

使用http_build_query():

<?php
{
  $postdata = array();

  //Variables to POST
  $postdata['access_token'] = "b34480a685e7d638d9ee3e53cXXXXX";
  $postdata['message']      = "hi";
  $postdata['send_to']      = "existing_contacts";

  //Initialize CURL data to send via POST to the API
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, "http://freesmsgateway.com/api_send");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postdata) );

  //Execute CURL command and return into variable $result
  $result = curl_exec($ch);

  //Do stuff
  echo "$result";
}
?>