如何在没有PECL_HTTP或CURL的PHP中使用http get请求?

时间:2021-02-20 06:32:08

I am putting up website & the domain hosting (1&1) only provide base PHP 5.4 (No external libraries like PECL_HTTP or CURL etc, No installed libraries ).

我正在建立网站&域名托管(1&1)只提供基础PHP 5.4(没有外部库,如PECL_HTTP或CURL等,没有安装的库)。

I can add PHP class files to my code. I am trying to do a Http/Https get on this URL $url ="https://www.googleapis.com/freebase/v1/search?query=chocolate&count=2";

我可以将PHP类文件添加到代码中。我正在尝试用Http/Https访问这个URL $ URL ="https://www.googleapis.com/freebase/v1/search?query=chocolate&count=2";

I have tried Snoopy, & at least 6 different class libraries from PHPClasses none of them return anything (result is blank), I don't know why? But they all return page results.

我已经尝试过Snoopy,并且至少有6个不同的类库从PHPClasses中没有返回任何东西(结果是空的),我不知道为什么?但它们都返回页面结果。

Can anyone suggest a PHP class library that I can include (and NOT any installed library) which can do a simple Http/Https get & return results.

任何人都可以建议一个PHP类库,我可以包括(而不是任何已安装的库),它可以执行简单的Http/Https get和返回结果。

3 个解决方案

#1


2  

Your best bet for full http functionality (including headers, etc.) is to use file functions with a stream context.

对于完整的http功能(包括头文件等),您最好的选择是使用带有流上下文的文件函数。

From php.net:

从php.net:

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
          "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp)

#2


2  

If you just want the content of this "file" you could use file_get_contents() (see first example). See @OkekeEmmanuelOluchukwu's comment on how to send header.

如果您只想要这个“文件”的内容,可以使用file_get_contents()(参见第一个示例)。请参阅@ okekekekeemmanueloluchukwu对如何发送标题的评论。

#3


0  

Are you looking for the page output?

您正在寻找页面输出吗?

As you probably have found, include something.php?foo=bar does not work as php actually looks for a file ending in ?for=bar, but you can set all the get variables like this:

您可能已经发现,包括一些东西。php?foo=bar不工作,因为php实际上是在寻找一个文件以=bar的形式结束,但是你可以设置所有的get变量:

function getRequest($url, $data) {
    foreach ($data as $key => $value) {
        $_GET[$key] = $value;
    }
    include $url;
}

However, get variables on "full paths" (can't think of the proper name) work, eg include 'http://something.com/foo.php?bar=baz'. You can find examples here http://php.net/manual/en/function.include.php of the "variable passing" and get variables on a non-local-system.

但是,在“完整路径”(不能考虑合适的名称)上获取变量,例如“http://something.com/foo.php?bar=baz”。您可以在这里找到示例http://php.net/manual/en/function.include.php的“变量传递”,并在非本地系统上获取变量。

#1


2  

Your best bet for full http functionality (including headers, etc.) is to use file functions with a stream context.

对于完整的http功能(包括头文件等),您最好的选择是使用带有流上下文的文件函数。

From php.net:

从php.net:

$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"Accept-language: en\r\n" .
          "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

$fp = fopen('http://www.example.com', 'r', false, $context);
fpassthru($fp);
fclose($fp)

#2


2  

If you just want the content of this "file" you could use file_get_contents() (see first example). See @OkekeEmmanuelOluchukwu's comment on how to send header.

如果您只想要这个“文件”的内容,可以使用file_get_contents()(参见第一个示例)。请参阅@ okekekekeemmanueloluchukwu对如何发送标题的评论。

#3


0  

Are you looking for the page output?

您正在寻找页面输出吗?

As you probably have found, include something.php?foo=bar does not work as php actually looks for a file ending in ?for=bar, but you can set all the get variables like this:

您可能已经发现,包括一些东西。php?foo=bar不工作,因为php实际上是在寻找一个文件以=bar的形式结束,但是你可以设置所有的get变量:

function getRequest($url, $data) {
    foreach ($data as $key => $value) {
        $_GET[$key] = $value;
    }
    include $url;
}

However, get variables on "full paths" (can't think of the proper name) work, eg include 'http://something.com/foo.php?bar=baz'. You can find examples here http://php.net/manual/en/function.include.php of the "variable passing" and get variables on a non-local-system.

但是,在“完整路径”(不能考虑合适的名称)上获取变量,例如“http://something.com/foo.php?bar=baz”。您可以在这里找到示例http://php.net/manual/en/function.include.php的“变量传递”,并在非本地系统上获取变量。