2015-02-09 183 views
0

我想用PHP讀取一個XML文件,但是我得到這個錯誤,不知道我做錯了什麼。用PHP讀取XML文件

Warning: file_get_contents(username:[email protected]://services.mobile.de/1.0.0/ad/search) [function.file-get-contents]: failed to open stream: No such file or directory in /www/htdocs/*******/mobile.php on line 11 

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /www/htdocs/*******/mobile.php:12 Stack trace: #0 /www/htdocs/w00f6d79/mobile.php(12): SimpleXMLElement->__construct('') #1 {main} thrown in /www/htdocs/*******/mobile.php on line 12 

我的代碼是:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>mobile.de</title> 
</head> 

<body> 
<?php 
$url = "username:[email protected]://services.mobile.de/1.0.0/ad/search"; 
$xml = file_get_contents($url); 
$data = new SimpleXMLElement($xml); 
echo $data->children('ad', true)->ad->vehicle->class->children()->children('resource', true)->local-description; 
?> 
</body> 
</html> 
+0

URL'用戶名:密碼@ http:// services.mobile.de/1.0.0/ad/search'不可訪問。 – 2015-02-09 12:40:02

+0

'file_get_contents'對creds一無所知。應該使用['curl'函數](http://php.net/manual/en/book.curl.php)。 – mudasobwa 2015-02-09 12:40:15

+0

您的文件爲空 – 2015-02-09 12:40:34

回答

0

,你正試圖獲得該文件沒有在服務器上找到。

1)您請求/重定向到文件,無論是從服務器返回的服務器 2)沒有被發現是無效的XML內容

請檢查該文件的位置和你的服務器配置。

0

username:[email protected]://services.mobile.de/1.0.0/ad/search不是有效的URL。某些HTTP客戶端了解http://username:[email protected]/1.0.0/ad/search,但身份驗證應該位於HTTP標頭中 - 而不是URL。

$authorization = base64_encode('username:password'); 
$options = array(
    'http'=>array(
    'method'=>"GET", 
    'header'=>"Authorization: Basic ".$authorization."\r\n" 
) 
); 
$context = stream_context_create($options); 
$data = file_get_contents(
    'http://services.mobile.de/1.0.0/ad/search', false, $context 
);