PHP-curl是';不行吗?
我正在尝试将SMS网关API与PHP结合使用。我的代码是:PHP-curl是';不行吗?,php,curl,Php,Curl,我正在尝试将SMS网关API与PHP结合使用。我的代码是: <?php // API integration if (isset($_POST['submit'])) { //Variables to POST $username = "user"; $password = "pass"; $msisdn = $_POST['phone']; $message = $_POST['message']; //Initiali
<?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>
电话号码(例如:919999999,用逗号分隔)
消息:
这不起作用,也没有响应
$result
。请告诉我出了什么问题。请先用这种方法检查您的问题,然后再进一步检查
$error=curl_error($ch); //print_r($error);
$header=curl_getinfo( $ch ); //print_r($header);
$result = curl_exec($ch); //print_r($result);
您的
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,...);
...
到
查看php.ini文件中的php_curl.dll,
若有,;删除它,如果可行,请重试。您应该将POST查询编码为字符串:
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('username' => $username,
'password' => $password,
'message' => $message,
'msisdn' => $msisdn), '', '&');
);
使用端口5567时,状态如下:
如果您在连接到我们时遇到与防火墙相关的问题,您可以返回端口80(但我们建议您花点时间允许通过防火墙对端口5567和7512进行传出访问)。为此,只需从URL中删除:5567(或类似内容)
因此,您可以尝试使用回退端口(80)。什么是
var\u转储($result)代码>给你?检查cURL中是否有任何错误,执行:if(cURL_errno($ch)){echo cURL_error($ch)}尝试通过删除来启用;从php.ini文件上的extension=php_curl.dll尝试将您的数据发布为@Sudhir suggested,因为它似乎对我来说运行良好。。除了响应是“无效凭据”之外,我已经解决了这个问题。但它仍然没有打印$Result。我不认为这会有任何区别。哇!我确实是这样做的,然后你评论说:p,解决了:)将端口更改为80我已经编辑了你的答案并对其进行了升级,因为问题已经记录在案。。。不过,这个问题没那么有用:)
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('username' => $username,
'password' => $password,
'message' => $message,
'msisdn' => $msisdn), '', '&');
);