繁体   English   中英

PHP- curl无法正常工作?

[英]PHP- curl isn't working?

我正在尝试通过PHP使用SMS网关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 请告诉我怎么了。

尝试这种方式先检查您的问题,然后再进一步前进

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

FAQ在使用端口5567时指出以下内容:

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

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

您的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;

看一下php.ini文件中的php_curl.dll(如果带有); 删除它,如果可行。

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

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

              );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM