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

发布于 2024-09-15 17:36:19 字数 1217 浏览 11 评论 0原文

我正在开发一个网络应用程序,用于从 160by2 等网站向手机发送短信。

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

未使用 fsockopen() 因为端口号未知,已向支持团队发送邮件以获取端口号。 (如果有任何通过 GET 方法进行 fsockopen 的代码被邀请:)。 )

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

欢迎任何帮助。

提前致谢。

编辑

有人可以告诉我除了cURL之外还有其他方式发送此HTTP请求吗?如果可能的话请给出代码。

我问这个问题是因为当前的 cURL 代码执行时间太长,并且在 60 秒后失败,我在本地系统的 php.ini 中将 max_execution_time 增加到 120,尽管这对我来说没有好处:( 。

I'm developing a web application for sending SMS to mobile from website like 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.

how to send the HTTP request from PHP?

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() 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?

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.

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 :( .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

岁月无声 2024-09-22 17:36:19

您的问题在于构建 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;

response.php:

<?php
print_r($_GET);

request.php 的输出是:

Array
(
    [foo] => yes
)

原因是查询字符串不正确编码,并且解释请求的服务器假定 URL 以第一个空格结束,在本例中,该空格位于查询的中间:foo=yes we can&baz=foo bar

您需要使用 http_build_query 构建 URL,这会照顾到正确对查询字符串进行 urlencode 编码通常会使代码看起来更具可读性:

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

您还需要设置 CURLOPT_RETURNTRANSFER:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

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

The output of request.php is:

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.

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

You also need to set CURLOPT_RETURNTRANSFER:

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
挽袖吟 2024-09-22 17:36:19
<?php
print file_get_contents("http://some_server/some_file.php?some_args=true");
?>
<?php
print file_get_contents("http://some_server/some_file.php?some_args=true");
?>
对你而言 2024-09-22 17:36:19

下面的代码将使用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;
?>

The below code will make a HTTP request with curl and return the response in $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;
?>
Hello爱情风 2024-09-22 17:36:19

我一眼就能看出你的代码没有任何问题。您是否尝试过将网址粘贴到浏览器中以便可以看到响应?您甚至可以使用 wget 将响应下载到文件中。我想如果你想尝试 fsocket,你会使用端口 80,因为这是默认的 http 端口。

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.

要走干脆点 2024-09-22 17:36:19

尝试使用以下代码。

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

确保您已加载 HttpRequest 扩展(在服务器中共享 object0)。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文