2016-05-05 14 views
1

別のサーバーからxmlファイルを解析したいと思います。phpを使用してurlからXMLファイルを解析する

<?php 

    $xml = simplexml_load_file('http://example_page.com/api/test.xml'); 

?> 

このコードは、このファイルがページと同じサーバーにある場合にのみ動作しますが、別のサーバーにxmlファイルがあります。 Webページから

警告:

Warning: simplexml_load_file(http://example_page.ugu.pl/api/test.xml) [function.simplexml-load-file]: failed to open stream: Connection refused in /virtual/z/y/example_page.ugu.pl/index.php on line 14 

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://example_page.com/api/test.xml" in /virtual/z/y/example_page.ugu.ugu.pl/index.php on line 14 

P.S. allow_url_fopenはまだオンです。

答えて

1

そこにいくつかのリダイレクトの問題は、あなたがアクセスしているURLであり、そしておそらくsimplexml_load_file()リダイレクトに従わないようなので、解決策がfile_get_contents()またはcUrlを使用することです

...ようです...

file_get_contents()として

が容易になり、私は...

コードのようなものでなければならないであろうことを示しています

<?php 

    $xml = simplexml_load_string(file_get_contents('http://example_page.ugu.pl/api/test.xml')); 

?> 

より:

<?php 

    $xml = simplexml_load_string(file_get_contents('http://jakdojade.pl/pages/api/outputs/schedules.xml')); 

?> 

^それも、完全に動作します!

+0

これを使用しても私はまだエラーがあります。警告:file_get_contents(http://example_page.com/api/test.xml)[function.file-get-contents]:ストリームを開くことに失敗しました:/virtual/z/y/examle_page.com/index.phpで接続が拒否されましたオンライン上のPS私はallow_url_fopenがオンであると確信しています。私はphpinfo()を使ってチェックしました。 – Zyngi

+0

理由は何もありませんが、あなたがアクセスしようとしているWebページは...下に...そして本当にtbhです。http://example_page.com/api/test.xmlにアクセスしようとしましたか?ブラウザ? – Ikari

+0

はい。ここに正しいリンクがあります。 http://jakdojade.pl/pages/api/outputs/schedules.xml – Zyngi

0

CURL(アクティブな場合)を使用する必要があります。

$url = "http://www.you-web-here.com/"; 
$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$xmlString = curl_exec($curl); 
+0

それは働きます!ありがとうございました! – Zyngi

+0

Curl;)を使用することをお勧めします。 – Grommy

0

PHPマニュアル:

Note: 
Libxml 2 unescapes the URI, so if you want to pass e.g. b&c as the URI parameter a, you have to call simplexml_load_file(rawurlencode('http://example.com/?a=' . urlencode('b&c'))). Since PHP 5.1.0 you don't need to do this because PHP will do it for you. 

Soがどのバージョンを使用していますか?

それとも、あなたは次のように試すことができます。

$xml_content = file_get_content('http://example_page.com/api/test.xml'); 
xml = simplexml_load_string($xml_content); 
+0

ああ、申し訳ありませんが、file_get_contentsではなく、file_get_contentであり、チャートを失っています – steve

+0

それはまた動作します!ありがとう! – Zyngi

関連する問題