简体   繁体   English

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

[英]how to send HTTP request by GET method in PHP to another website

I'm developing a web application for sending SMS to mobile from website like 160by2 . 我正在开发一个Web应用程序,用于从160by2等网站向移动设备发送短信。

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 . 我可以准备HTTP GET请求所需的URL,如SMS Gateway提供商smslane.com提供的API中所述,这里是API链接

how to send the HTTP request from PHP? 如何从PHP发送HTTP请求?

I used cURL for that purpose but the response is not displayed. 我为此目的使用了cURL,但没有显示响应。 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() was not used because port number unknown, mailed the support team for port number. 由于端口号未知,因此未使用fsockopen() ,请向支持团队邮寄端口号。 (if any code for fsockopen through GET method is invited :). (如果邀请任何通过GET方法的fsockopen代码:)。 )

are there any other methods for doing this? 这有什么其他方法吗?

any help is welcome. 欢迎任何帮助。

Thanks in advance. 提前致谢。

EDIT 编辑

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之外还有其他任何方式发送此HTTP请求,如果可能的话,还可以为此提供代码。

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

Your problem is the way you are constructing the URL. 您的问题是您构建URL的方式。 The spaces you are including in the query string will result in a malformed request URL being sent. 您在查询字符串中包含的空格将导致发送格式错误的请求URL。

Here is an example that replicates your circumstances: 这是一个复制您的情况的示例:

request.php : 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 : response.php

<?php
print_r($_GET);

The output of request.php is: request.php的输出是:

Array
(
    [foo] => yes
)

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

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: 您需要使用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
));

You also need to set CURLOPT_RETURNTRANSFER: 您还需要设置CURLOPT_RETURNTRANSFER:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
<?php
print file_get_contents("http://some_server/some_file.php?some_args=true");
?>

The below code will make a HTTP request with curl and return the response in $content . 下面的代码将使用curl发出HTTP请求并返回$content的响应。

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,'http://www.anydomain.com/anyapi.php?a=parameters');
$content = curl_exec($ch);
echo $content;
?>

nothing wrong with your code that I can see right off. 你的代码没有任何问题,我可以立即看到。 have you tried pasting the url into a browser so you can see the response? 您是否尝试将网址粘贴到浏览器中以便查看响应? you could even use wget to download the response to a file. 您甚至可以使用wget将响应下载到文件中。 I suppose if you want to try fsocket that you would use port 80 as that's the default http port. 我想如果你想尝试使用端口80的fsocket,那就是默认的http端口。

Try using following code. 尝试使用以下代码。

$r = new HttpRequest('http://www.xencomsoftware.net/configurator/tracker/ip.php', HttpRequest::METH_GET);
try {
        $r->send();
         echo $r->getResponseCode(); 
         if ($r->getResponseCode() == 200) 
        {
        }
    }

make sure that you have loaded HttpRequest extension (share object0 in your server. 确保已加载HttpRequest扩展(在服务器中共享object0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM