SimpleXML hata işleme php

5 Cevap

Ben aşağıdaki kodu kullanıyorum:

function GetTwitterAvatar($username){
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
$imgurl = $xml->profile_image_url;
return $imgurl;
}

function GetTwitterAPILimit($username, $password){
$xml = simplexml_load_file("http://$username:$password@twitter.com/account/rate_limit_status.xml");
$left = $xml->{"remaining-hits"};
$total = $xml->{"hourly-limit"};
return $left."/".$total;
}

akış bağlanamıyor ve bu hataları alıyorsanız:

Warning: simplexml_load_file(http://twitter.com/users/****.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://twitter.com/users/****.xml" 

Warning: simplexml_load_file(http://...@twitter.com/account/rate_limit_status.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://***:***@twitter.com/account/rate_limit_status.xml"

Bunun yerine yukarıda gösterilmiştir ne bir kullanıcı dostu bir mesaj görüntüleyebilir, böylece Bu hataları nasıl işleyebilir?

5 Cevap

Dokümantasyon, bir hata durumunda, simplexml_load_file FALSE döndürür söylüyor. Yani, bir koşullu deyimi ile birlikte "kapa çeneni" operatörü (@) kullanabilirsiniz:

if (@simplexml_load_file($file))
{
    // continue
}
else 
{
    echo 'Error!';
}

Ben bu daha iyi bir yol olduğunu düşünüyor

$use_errors = libxml_use_internal_errors(true);
$xml = simplexml_load_file($url);
if (!$xml) {
  //throw new Exception("Cannot load xml source.\n");
}
libxml_clear_errors();
libxml_use_internal_errors($use_errors);

Daha fazla bilgi: http://php.net/manual/en/function.libxml-use-internal-errors.php

Ben php documentation güzel bir örnek buldum.

So the code is:

libxml_use_internal_errors(true);
$sxe = simplexml_load_string("<?xml version='1.0'><broken><xml></broken>");
if (!$sxe) {
    echo "Başarısız yükleme XML\n";
    foreach(libxml_get_errors() as $error) {
        echo "\t", $error->message;
    }
}

And the output, as we/I expected:

Başarısız yükleme XML

Blank needed here
parsing XML declaration: '?>' expected
Opening and ending tag mismatch: xml line 1 and broken
Premature end of data in tag broken line 1

Eğer kılavuzunu bakarsanız, bir opsiyon parametre vardır:

SimpleXMLElement simplexml_load_file ( string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] )

Seçenekler listesi mevcuttur: http://www.php.net/manual/en/libxml.constants.php

Bu uyarıları ayrıştırma uyarıları bastırmak için doğru yolu:

$xml = simplexml_load_file('file.xml', 'SimpleXMLElement', LIBXML_NOWARNING);

if (simplexml_load_file($file) !== false) { // continue } else { echo 'Error!'; }

Ve Twitter is down, belki?