如何通过php中的GET方法将HTTP请求发送到另一个网站 [英] how to send HTTP request by GET method in PHP to another website

查看:141
本文介绍了如何通过php中的GET方法将HTTP请求发送到另一个网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Web应用程序,用于从 160by2 这样的网站向手机发送SMS.

I'm developing a web application for sending SMS to mobile from website like 160by2.

我可以准备SMS网关提供商提供的API中提到的HTTP GET请求所需的URL smslane.com ,这是该API的链接.

I can prepare the URL required for the HTTP GET request as mentioned in the API provided by the SMS Gateway provider smslane.com, here is the link for the API.

如何从PHP发送HTTP请求?

how to send the HTTP request from PHP?

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

I used cURL for that purpose but the response is not displayed. here is the code i used,

<?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;
?>

未使用

fsockopen(),因为端口号未知,因此向支持团队发送了端口号. (如果邀请通过GET方法进行fsockopen的任何代码,请:). )

fsockopen() was not used because port number unknown, mailed the support team for port number. (if any code for fsockopen through GET method is invited :). )

还有其他方法可以做到吗?

are there any other methods for doing this?

欢迎任何帮助.

先谢谢了.

编辑

除了 cURL 之外,任何人都可以告诉我是否还有其他发送此HTTP请求的方法,并在可能的情况下提供代码.

Could any one tell me is there any other way to send this HTTP Request except cURL and if possible give code for that.

我之所以这样问,是因为当前的cURL代码执行时间过多,并且在60秒后失败,因此即使本地系统上的cURL代码对我没有好处,我也会将其在php.ini中的max_execution_time增加至120.

I'm asking this because the current cURL code taking too much time for execution and fails after 60 seconds, i increased max_execution_time to 120 in php.ini on my local system even though it is not doing good for me :( .

推荐答案

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

Your problem is the way you are constructing the URL. The spaces you are including in the query string will result in a malformed request URL being sent.

以下是一个可以复制您的情况的示例:

Here is an example that replicates your circumstances:

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;

response.php :

<?php
print_r($_GET);

request.php的输出是:

The output of request.php is:

Array
(
    [foo] => yes
)

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

The reason for this is the query string is not properly encoded and the server interpreting the request assumes the URL ends at the first space, which in this case is in the middle of the query: foo=yes we can&baz=foo bar.

您需要使用 http_build_query 构建URL.会妥善地对您的查询字符串进行urlencoding,通常会使代码看起来更具可读性:

You need to build your URL using http_build_query, which will take care of urlencoding your query string properly and generally makes the code look a lot more readable:

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

您还需要设置CURLOPT_RETURNTRANSFER:

You also need to set CURLOPT_RETURNTRANSFER:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

这篇关于如何通过php中的GET方法将HTTP请求发送到另一个网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆