php 如何通过PHP中的GET方法将HTTP请求发送到另一个网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3528530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to send HTTP request by GET method in PHP to another website
提问by srinivas
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 网关提供商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. here is the code i used,
我为此目的使用了 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;
?>
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 :). )
fsockopen()
未使用,因为端口号未知,向支持团队邮寄了端口号。(如果通过 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 cURLand 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,即使它对我没有好处:(。
采纳答案by Shabbyrobe
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.
您的问题是您构建 URL 的方式。您在查询字符串中包含的空格将导致发送格式错误的请求 URL。
Here is an example that replicates your circumstances:
这是一个复制您的情况的示例:
request.php:
请求.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:
<?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);
回答by rook
<?php
print file_get_contents("http://some_server/some_file.php?some_args=true");
?>
回答by Yogesh V Patil
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;
?>
回答by Al W
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. I suppose if you want to try fsocket that you would use port 80 as that's the default http port.
你的代码没有问题,我可以马上看到。您是否尝试将 url 粘贴到浏览器中以便查看响应?您甚至可以使用 wget 将响应下载到文件中。我想如果你想尝试 fsocket,你会使用端口 80,因为这是默认的 http 端口。
回答by Elixir Techne
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。