0

我在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 网关告知)。但正如您所看到的,我的消息字段不是空白的。

4

2 回答 2

0

我相信您不能将数组发送到 CURLOPT_POSTFIELDS,您需要将行替换为以下内容

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

我希望这能解决它

于 2013-07-28T11:14:25.780 回答
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";
}
?>
于 2013-07-29T07:58:28.690 回答