MAMP php 5.6.2 file_get_contents无效(超时)

时间:2014-12-18 21:01:23

标签: php mamp

我已经搜索过,找不到这个原因无效的原因。我试过从命令行和apache执行,但它不起作用。

<?php
echo file_get_contents("http://google.com");
?>

我有一个简单的PHP脚本,如上所示,它只是向谷歌发出请求并输出内容。

错误:

Warning: file_get_contents(http://google.com): failed to open stream: Operation timed out in /Applications/MAMP/htdocs/p.php on line 2

PHP版本:    PHP 5.6.2(cli)(建于2014年10月20日16:21:27)

allow_url_fopen = On

奇怪的是,如果我使用/ usr / bin / php它运行正常。 (内置苹果版)

这是wireshark捕获: https://www.dropbox.com/s/u7jdg494fmud22i/packet_capture_php_network.pcapng?dl=0

2 个答案:

答案 0 :(得分:0)

我建议您使用CURL替代方案:

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

答案 1 :(得分:0)

您的apache服务器可能有限制。查看此post详细说明修复程序。应该只是在php.ini文件中进行配置更改(确保更改MAMP PHP的php.ini文件)。