如何通过PHP中的GET方法将HTTP请求发送到另一个网站

sri*_*vas 11 php sms http-get httprequest sms-gateway

我正在开发一个Web应用程序,用于从160by2等网站向移动设备发送短信.

我可以准备HTTP GET请求所需的URL,如SMS Gateway提供商smslane.com提供的API中所述,这里是API链接.

如何从PHP发送HTTP请求?

我为此目的使用了cURL,但没有显示响应.这是我用过的代码,

<?php 
$url="http://smslane.com/vendorsms/pushsms.aspx?user=abc&password=xyz&msisdn=919898123456&sid=WebSMS&msg=Test message from SMSLane&fl=0";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
curl_setopt( $ch, CURLOPT_URL, $url );  
$content = curl_exec( $ch );
$response = curl_getinfo( $ch );
curl_close ( $ch );
echo $content;
?>
Run Code Online (Sandbox Code Playgroud)

fsockopen()没有使用,因为端口号未知,邮寄支持团队的端口号.(如果邀请任何通过GET方法的fsockopen代码:).)

这有什么其他方法吗?

欢迎任何帮助.

提前致谢.

编辑

任何人都可以告诉我除了cURL之外还有其他任何方式发送此HTTP请求,如果可能的话,还可以为此提供代码.

我问这是因为当前的cURL代码花了太多时间执行并在60秒后失败,我在本地系统上的php.ini中将max_execution_time增加到120,即使它对我没有好处:(.

Sha*_*obe 11

您的问题是您构建URL的方式.您在查询字符串中包含的空格将导致发送格式错误的请求URL.

这是一个复制您的情况的示例:

request.php:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 
    'http://your_server/response.php?foo=yes we can&baz=foo bar'
);
$content = curl_exec($ch);
echo $content;
Run Code Online (Sandbox Code Playgroud)

response.php:

<?php
print_r($_GET);
Run Code Online (Sandbox Code Playgroud)

request.php的输出是:

Array
(
    [foo] => yes
)
Run Code Online (Sandbox Code Playgroud)

原因是查询字符串没有正确编码,服务器解释请求假定URL在第一个空格结束,在这种情况下是在查询的中间:foo=yes we can&baz=foo bar.

您需要使用http_build_query构建您的URL ,它将正确地对您的查询字符串进行urlencoding,并且通常使代码看起来更具可读性:

echo http_build_query(array(
    'user'=>'abc',
    'password'=>'xyz',
    'msisdn'=>'1234',
    'sid'=>'WebSMS',
    'msg'=>'Test message from SMSLane',
    'fl'=>0
));
Run Code Online (Sandbox Code Playgroud)

您还需要设置CURLOPT_RETURNTRANSFER:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
Run Code Online (Sandbox Code Playgroud)


roo*_*ook 7

<?php
print file_get_contents("http://some_server/some_file.php?some_args=true");
?>
Run Code Online (Sandbox Code Playgroud)