2017-02-14 110 views
1

有人可以帮我鉴定在这里的错误:与文件PHP错误获取内容

<?php 
$city=$_GET['city']; 
$city=str_replace(" ", "", $city); 
[email protected]_get_contents("http://www.weather-forecast.com/locations/'.$city'/forecasts/latest"); 
preg_match('/<span class="phrase">(.*?)</s', $contents, $matches); 
echo @$matches[1]; 
?> 

出于某种原因的代码是给我的错误,但是,如果我删除了城市和特定城市进行更换(伦敦,巴黎,开罗)的代码工作,但是当我把它放在城市..代码不起作用。

有人可以帮助我确定错误或如何工作。

+0

改变这个'“http://www.weather-forecast.com/locations/'.$city'/forecasts/latest”''到“http://www.weather-forecast.com/locations/$city/forecasts/latest”'。然后尝试 –

+0

重新格式化您的问题(不需要粗体)。在执行$ contents行之前,$ city的价值是多少? –

+0

字符串连接出现错误。 –

回答

0

首先从您的所有代码行中删除@(不要从错误中逃脱,尝试查找并解决它们)。

第二个变化$contents线象下面这样: -

$contents= file_get_contents("http://www.weather-forecast.com/locations/$city/forecasts/latest"); 

或者

$contents= file_get_contents("http://www.weather-forecast.com/locations/'".$city."'/forecasts/latest"); 

注: - 字符串连接问题是有在你的代码

+0

谢谢..现在它工作...我用你的$城市和工作。 – Farida

+0

@法里达很高兴帮助你。 –

0
<?php 
$city=$_GET['city']; 
$city=str_replace(" ", "", $city); 
$contents=file_get_contents('http://www.weather-forecast.com/locations/'.$city.'/forecasts/latest'); 
preg_match('/<span class="phrase">(.*?)</s', $contents, $matches); 
echo $matches[1]; 
?> 
+3

请不要只是邮编,而是说明关键的变化。 – mkl

0

能否请您检查$城市变量的分析是这样

$contents = @file_get_contents("http://www.weather-forecast.com/locations/".$city."/forecasts/latest");