处理 https 的 sendToHost() 函数的替代方案

发布于 2024-12-19 16:52:45 字数 1714 浏览 1 评论 0原文

我一直在使用一个名为 sendToHost 的函数,它可以使用 GET 字符串 ping 服务器并返回响应。

#  Example of GET string:
#  sub.domain.com/api.php?a=1&b=2&c=3
$var = sendToHost('sub.domain.com','get','/api.php','a=1&b=2&c=3');

但是,它不会处理发送到安全服务器 (https) 的情况。

有谁知道sendToHost的php函数替代品吗?

这是 SendToHost 代码:

/* sendToHost
 * ~~~~~~~~~~
 * Params:
 *   $host - Just the hostname.  No http:// or /path/to/file.html portions
 *   $method - get or post, case-insensitive
 *   $path - The /path/to/file.html part
 *   $data - The query string, without initial question mark
 *   $useragent - If true, 'MSIE' will be sent as the User-Agent (optional)
 *
 * Examples:
 *   sendToHost('www.google.com','get','/search','q=php_imlib');
 *   sendToHost('www.example.com','post','/some_script.cgi',
 *              'param=First+Param&second=Second+param');
 */
function sendToHost($host,$method,$path,$data,$useragent=0)
{
    // Supply a default method of GET if the one passed was empty
    if (empty($method))
        $method = 'GET';
    $method = strtoupper($method);
    $fp = fsockopen($host,80);
    if ($method == 'GET')
        $path .= '?' . $data;
    fputs($fp, "$method $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
    if ($method == 'POST')
        fputs($fp, "Content-length: " . strlen($data) . "\r\n");
    if ($useragent)
        fputs($fp, "User-Agent: MSIE\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    if ($method == 'POST')
        fputs($fp, $data);

    while (!feof($fp))
        $buf .= fgets($fp,128);
    fclose($fp);
    return $buf;
}

谢谢。

I've been using a function called sendToHost that has worked for pinging a server with a GET string and returning a response.

#  Example of GET string:
#  sub.domain.com/api.php?a=1&b=2&c=3
$var = sendToHost('sub.domain.com','get','/api.php','a=1&b=2&c=3');

However it won't handle sending to a Secure Server (https).

Does anyone know of a php function alternative to sendToHost?

Here's the SendToHost code:

/* sendToHost
 * ~~~~~~~~~~
 * Params:
 *   $host - Just the hostname.  No http:// or /path/to/file.html portions
 *   $method - get or post, case-insensitive
 *   $path - The /path/to/file.html part
 *   $data - The query string, without initial question mark
 *   $useragent - If true, 'MSIE' will be sent as the User-Agent (optional)
 *
 * Examples:
 *   sendToHost('www.google.com','get','/search','q=php_imlib');
 *   sendToHost('www.example.com','post','/some_script.cgi',
 *              'param=First+Param&second=Second+param');
 */
function sendToHost($host,$method,$path,$data,$useragent=0)
{
    // Supply a default method of GET if the one passed was empty
    if (empty($method))
        $method = 'GET';
    $method = strtoupper($method);
    $fp = fsockopen($host,80);
    if ($method == 'GET')
        $path .= '?' . $data;
    fputs($fp, "$method $path HTTP/1.1\r\n");
    fputs($fp, "Host: $host\r\n");
    fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
    if ($method == 'POST')
        fputs($fp, "Content-length: " . strlen($data) . "\r\n");
    if ($useragent)
        fputs($fp, "User-Agent: MSIE\r\n");
    fputs($fp, "Connection: close\r\n\r\n");
    if ($method == 'POST')
        fputs($fp, $data);

    while (!feof($fp))
        $buf .= fgets($fp,128);
    fclose($fp);
    return $buf;
}

Thanks.

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

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

发布评论

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

评论(1

ぃ弥猫深巷。 2024-12-26 16:52:45

你尝试过使用curl吗?它支持 ssl 并且在 GNU/Linux 下运行良好。

Have you tried using curl? It supports ssl and works well under GNU/Linux..

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