PHP-curl不起作用?

时间:2014-06-13 06:11:40

标签: php curl

我正在尝试使用PHP的SMS Gateway API。我的代码是:

<?php
 // API integration
 if (isset($_POST['submit'])) {

     //Variables to POST
     $username = "user";
     $password = "pass";
     $msisdn = $_POST['phone'];
     $message = $_POST['message'];

     //Initialize CURL data to send via POST to the API
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, "http://bulksms.vsms.net:5567/eapi/submission/send_sms/2/2.0");
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_POSTFIELDS,
              array('username' => $username,
                    'password' => $password,
                    'message' => $message,
                    'msisdn' => $msisdn)
              );

     //Execute CURL command and return into variable $result

     $result = curl_exec($ch);
     echo "$result";
 }
?>
 <form name='sms' action='' method='post'>
 Phone number (eg.: 919999999999, separated by commas) <br/><input type='text' name='phone' value=''/>
 Message: <br/><input type='textarea' name='message' value=''/> 
 <br/>
 <input type='submit' name='submit' value='Send SMS'>
 </form>

这不起作用,它都没有回应$result。请告诉我出了什么问题。

5 个答案:

答案 0 :(得分:1)

首先尝试检查问题,然后再继续前进

$error=curl_error($ch);    //print_r($error); 
$header=curl_getinfo( $ch ); //print_r($header);
$result = curl_exec($ch);  //print_r($result);

答案 1 :(得分:1)

FAQ使用端口5567说明以下内容:

  

如果您遇到与我们相关的防火墙问题,您可以回退到端口80(但我们建议您花点时间通过防火墙允许对端口5567和7512的传出访问)。为此,只需从URL中删除:5567(或类似)。

因此,您可以尝试后备端口(80)。

答案 2 :(得分:0)

您的CURLOPT_POSTFIELDS无效,请执行:

.....
$postData = array('username' => $username,
    'password' => $password,
    'message' => $message,
    'msisdn' => $msisdn
);

//create post body  
$post_body = '';
foreach( $postData as $key => $value ) {
    $post_body .= urlencode( $key ).'='.urlencode( $value ).'&';
}
$post_body = rtrim( $post_body,'&' );

并更改以下行:

...
curl_setopt($ch, CURLOPT_POSTFIELDS,...);
...

..
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
...
echo $result;

答案 3 :(得分:0)

看一下php.ini文件中的php_curl.dll, 如果它有;删除它尝试它是否有效。

答案 4 :(得分:0)

您应该将POST查询编码为字符串:

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('username' => $username,
                    'password' => $password,
                    'message' => $message,
                    'msisdn' => $msisdn), '', '&');

              );