简体   繁体   中英

How to fix "simplexml_load_file" error in PHP

I'm using this code to get Alexa rank in my local server. but this code isn't work when I'm upload to my server and i get errors.

Code I'm using:

$uri = 'http://data.alexa.com/data?cli=10&dat=snbamz&url=';
$uri .= $domain;
$xml = simplexml_load_file($uri);
if (isset($xml->SD[1]->COUNTRY))
$country_rate = (int) $xml->SD[1]->COUNTRY->attributes()->RANK;
return($country_rate);

Errors:

Warning: simplexml_load_file(): http://data.alexa.com/data?cli=10&dat=snbamz&url=tooc.ir:1: parser error : Start tag expected, '<' not found in /home2/mydesign/domains/tooc.ir/public_html/panel/2.php on line 5
Warning: simplexml_load_file(): Okay in /home2/mydesign/domains/tooc.ir/public_html/panel/2.php on line 5
Warning: simplexml_load_file(): ^ in /home2/mydesign/domains/tooc.ir/public_html/panel/2.php on line 5
Notice: Undefined variable: country_rate in /home2/mydesign/domains/tooc.ir/public_html/panel/2.php on line 13

and here is the link of the sample page.

Alexa add my IP to the blacklist and because of it the response is "Okay". I'm find the minisiteinfo URL and writing new code to get global and country ranking and its work fine at this time. i will put down the code if anybody have problem like me.

CODE:

function AlexaRank($domain, $country, $mode) {
    $url = "https://www.alexa.com/minisiteinfo/".$domain;
    $string = file_get_contents($url);
    if ($mode == "country") {
        $temp_s = substr($string, strpos($string, $country." Flag") + 9 + strlen($country));
        return(substr($temp_s, 0, strpos($temp_s, "</a></div>")));
    }
    else if ($mode == "global") {
        $temp_s = substr($string, strpos($string, "Global") + 38);
        return(substr($temp_s, 0, strpos($temp_s, "</a></div>")));
    }
    else {
        return('something wrong.');
    }
}


// Here how to use for country rank:
echo AlexaRank("tooc.ir", "Iran", "country");

// Here how to use for global rank:
echo AlexaRank("tooc.ir", "Iran", "global");

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