I tried to send an http url using curl but it didn't work at all, here is the code I've used:
我尝试使用curl发送一个http url,但它根本不起作用,这是我用过的代码:
<?php
$username = 'username';
$password = 'password';
$sender='sender';
$msisdn = '+96899999999';
$content="Test MSG";
$data = "username=".$username."&password=".$password."&message=".urlencode($content)."&sender=".$sender."&msisdn=".urlencode($msisdn);
$ch = curl_init('http://bulksms.vsms.net/eapi/submission/send_sms/2/2.0');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($response = curl_exec($ch))
{
echo "1";
}
else
{
echo "0";
}
curl_close ($ch);
?>
It is always returning 0. What could be the problem?
它总是返回0.可能是什么问题?
1 个解决方案
#1
Take a look at the BulkSMS API documentation for PHP. That provides comprehensive code for utilizing Curl to send a SMS.
查看PHP的BulkSMS API文档。这提供了利用Curl发送SMS的综合代码。
Upon a quick inspection I see that they have a curl_setopt for the Port (443) and that their POST data is slightly different. The main difference is that yours has a sender
field whereas their's does not.
经过快速检查后,我发现他们的端口(443)有一个curl_setopt,他们的POST数据略有不同。主要区别在于你的发件人有发件人字段,而他们没有。
$ch = curl_init( );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_PORT, $port ); //443
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_body );
// Allowing cUrl funtions 20 second to execute
curl_setopt ( $ch, CURLOPT_TIMEOUT, 20 );
// Waiting 20 seconds while trying to connect
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
$response_string = curl_exec( $ch );
$curl_info = curl_getinfo( $ch );
I would recommend just starting off with their code and trimming off what is not needed.
我建议刚开始使用他们的代码并修剪掉不需要的东西。
#1
Take a look at the BulkSMS API documentation for PHP. That provides comprehensive code for utilizing Curl to send a SMS.
查看PHP的BulkSMS API文档。这提供了利用Curl发送SMS的综合代码。
Upon a quick inspection I see that they have a curl_setopt for the Port (443) and that their POST data is slightly different. The main difference is that yours has a sender
field whereas their's does not.
经过快速检查后,我发现他们的端口(443)有一个curl_setopt,他们的POST数据略有不同。主要区别在于你的发件人有发件人字段,而他们没有。
$ch = curl_init( );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_PORT, $port ); //443
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_body );
// Allowing cUrl funtions 20 second to execute
curl_setopt ( $ch, CURLOPT_TIMEOUT, 20 );
// Waiting 20 seconds while trying to connect
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 20 );
$response_string = curl_exec( $ch );
$curl_info = curl_getinfo( $ch );
I would recommend just starting off with their code and trimming off what is not needed.
我建议刚开始使用他们的代码并修剪掉不需要的东西。