[英]PHP- curl isn't working?
I am trying to use an SMS Gateway API with PHP. 我正在尝试通过PHP使用SMS网关API。 My code is:
我的代码是:
<?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>
This isn't working, neither it is echoing the $result
. 这是行不通的,也不是在呼应
$result
。 Please tell me what's wrong. 请告诉我怎么了。
Try this way to check out your problem first and then further step forward 尝试这种方式先检查您的问题,然后再进一步前进
$error=curl_error($ch); //print_r($error);
$header=curl_getinfo( $ch ); //print_r($header);
$result = curl_exec($ch); //print_r($result);
The FAQ states the following on using port 5567: FAQ在使用端口5567时指出以下内容:
You can fall back to port 80 if you have firewall-related problems connecting to us (but we recommend that you rather take the time to allow outgoing access to ports 5567 and 7512 via your firewall).
如果您遇到与防火墙相关的问题,可以退回到端口80(但我们建议您花一些时间允许通过防火墙对端口5567和7512进行出站访问)。 To do so, simply remove :5567 (or similar) from the URL.
为此,只需从URL中删除:5567(或类似名称)。
Therefore, you could try the fallback port (80). 因此,您可以尝试使用备用端口(80)。
your CURLOPT_POSTFIELDS
is invalid, do: 您的
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,'&' );
and change following line: 并更改以下行:
...
curl_setopt($ch, CURLOPT_POSTFIELDS,...);
...
to 至
..
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
...
echo $result;
Have a look at the php_curl.dll in your php.ini file, if it has a ; 看一下php.ini文件中的php_curl.dll(如果带有); remove it try if it works.
删除它,如果可行。
You should encode POST query into string: 您应该将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.