Uzak sayfanın değerini almak ve yerel değişkeni saklamak

2 Cevap

Ben bir tek xml değer döndüren bir uzak sunucuda bir sayfa var

<?xml version="1.0" ?><Tracker>12345</Tracker>

How do I go about getting the value 12345 or whatever is in the tag into a PHP variable? I know I can't do something as simple as:

<?php
    $var = http://www.www.com/file.php;
    echo $var;   //12345
?>

Ben bir web hizmeti yaratma fikrinden tümüyle yeni kulüpler - Ben bu olması gerektiğini düşünüyorum.?.

2 Cevap

Iki adımda gitmek zorunda:

Bu iki adımlar sunucu yapılandırma bunu izin veriyorsa, simplexml_load_file kullanarak, biri olarak birleştirilmiş olabilir unutmayın.


Once you have that XML in a PHP variable, using SimpleXML, it's quite easy to get the value you want. For instance :

$string = '<?xml version="1.0" ?><Tracker>12345</Tracker>';
$xml = simplexml_load_string($string);
echo (string)$xml;

Alırsınız

12345


And getting the content of that XML from the remote URL can be as simple as :

$string = file_get_contents('http://your-remote-url');

Bunu eğer Ve, aynı zamanda SimpleXML ile doğrudan o uzak URL kullanabilirsiniz:

$xml = simplexml_load_file('http://your-remote-url');
echo (string)$xml;

allow_url_fopen sunucunun yapılandırma etkinse Ama bu sadece çalışır.


If allow_url_fopen is disabled on your server, you can get the XML from the remote server using curl ; something like this should do the trick, for instance :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://your-remote-url");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$string = curl_exec($ch);
curl_close($ch);

Benim ilk örnekte olduğu gibi, sonra, yine, simplexml_load_string kullanabilirsiniz.

(If using curl, take a look at the documentation of curl_setopt : there are several options that might interest your -- for instance, to specify timeouts)

Ben iyi PHP biliyorum, ama yok:

  1. XML dosyasının içeriğini indirin - uzak konumda için okumak
  2. XML Ayrıştırma
  3. Bir varianble ayrıştırılmış öğenin değerini saklamak