简体   繁体   中英

Reading XML file with PHP

I'm trying to read an XML file with PHP but I get this error and don't know what I've done wrong.

Warning: file_get_contents(username:password@http://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

My code is:

<!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:password@http://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>

The file that your are trying to get is not found on the server.

1) The file that you requested / redirected to, was not found on the server 2) Whatever that is returned from the server was not valid XML content

Please check the file's location and your server configuration.

username:password@http://services.mobile.de/1.0.0/ad/search is not a valid URL. Some HTTP clients understand http://username:password@services.mobile.de/1.0.0/ad/search , but the the authentication should be in the HTTP headers - not the 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
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM