致命错误:调用未定义的函数http_get()

7

我尝试在PHP编码中使用http_get进行跨域请求。但是,在我的服务器上,没有安装所需的模块。我不知道要安装什么才能使http_get操作变为可能。

我收到了以下错误:

致命错误: 在 C:\xampp\htdocs\article\index.php 的第2行调用未定义函数http_get()

我尝试使用PECL pecl_http >= 0.1.0进行如下操作:

http_get — 执行GET请求

但是,我没有找到解决方案。

因此,请帮助我运行http_get编码。谢谢!

2个回答

5

我认为您需要在php.ini文件中启用extension=php_http.dll,然后重新启动Apache服务器。
我建议您使用cURL而不是http_get()(操作相同,您需要启用extension=php_curl.dll并重新启动Apache)

希望能有所帮助 :)


出于好奇,为什么您建议他们使用cURL呢? - James
@Halayem,你能给cURL提供一个使用示例吗?http_get()仍未定义。 - n8bar

3
直接回答问题,您需要在php.ini中启用extension=php_http.dll并重新启动apache,但现在绝对没有必要使用http_get()
以下是三个良好的替代方法:
  1. php curl
  2. file_get_contents()
  3. copy()

Curl用法:
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'http://testcURL.com/?item1=value&item2=value2',
    CURLOPT_USERAGENT => 'User Agent X'
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

file_get_content() 的用法:

$my_var = file_get_contents("https://site/");

copy() 的使用方法:

copy("https://site/image.png", "/local/image.png");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接