2010-05-21 113 views
1

,为什么我没有得到有关的file_get_contents($ myurl)回报,但我得到的输出wget的file_get_contents()没有返回?

更新:

我使用curl得到回报头和结果和发现的是,它是在头替代在身体

HTTP/1.1 200 Document follows 
Transfer-Encoding: chunked 
Content-type: text/plain 
Server: XMS (724Solutions HTA XSAM_30_M2_B020 20070803.172831) 
Date: Fri, 21 May 2010 10:48:31 GMT 
Accept-Ranges: bytes 

HTTP/1.1 404 ChargedPartyNotAvailable 
Transfer-Encoding: chunked 
Content-type: text/plain 
Server: XMS (724Solutions HTA XSAM_30_M2_B020 20070803.172831) 
Date: Fri, 21 May 2010 10:34:13 GMT 
Accept-Ranges: bytes 

我怎样才能提取出“200 Document Follow”和“404 ChargedPartyNotAvailable”?

+1

请显示一些代码,并更详细地解释你在做什么。 – 2010-05-21 09:26:45

回答

5

您是否已在您的php配置中启用allow_url_fopen

但是,我希望你会得到一个警告,如果没有 - 你有错误/警告显示?您可以暂时在脚本顶部添加:

error_reporting(E_ALL); 
ini_set('display_errors', true); 

然后您可能会看到为什么file_get_contents()不起作用。

编辑

你可能只是能够使用get_headers()如果你只是对标题感兴趣。

+0

+1为allow_url_fopen和error_reporting建议(虽然我只会记录错误:p)。 – wimvds 2010-05-21 09:39:32

+0

配置没问题。它似乎是结果出现在标题不在正文 – conandor 2010-05-21 11:26:36

+0

@conandor:基于您的编辑更新的答案 – 2010-05-21 12:50:51

3

您可以使用PHP卷曲包来获取URL的内容

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$myUrl); 
$result = curl_exec($ch); 
curl_close($ch); 

或者,如果你希望只使用的file_get_contents,请检查您是否正确配置的php.ini

http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

由于(http://php.net/manual/en/function.file-get-contents.php

URL可以用作fi如果已启用打开包装 ,则使用 这个函数。有关如何指定 文件名的更多详细信息,请参阅fopen() 。

+0

+1卷曲替代解决方案 – wimvds 2010-05-21 09:40:02

+0

PHP配置没问题。它似乎只出现在头部而不是正文 – conandor 2010-05-21 11:40:55

+0

对于那些想要使用变量'$ result'的用户,首先在'$ result = curl_exec($ ch)'之前加上'curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);'。因为它是这样的,变量是无用的。但对于短卷曲仍然是+1。 – 2017-01-04 08:52:50

0

你可以尝试:

$contents = implode('', file($myurl)); 

我经常用它。