Simplexml_load_file empty array - php

I try to load this document:
$url = "http://en.wikipedia.org/w/api.php?action=query&titles=Electrophoresis&prop=langlinks&lllimit=500";
When I run it in browser, everything is fine.
When I do this:
ini_set('user_agent', 'XX123456789 (localhost; myemailaddress)'); //sets info for authentication
$content = file_get_content($url);
var_dump($content);
It return the same xml document as my browser show.
However when I try to
$content_arrays = Simplexml_load_file($content);
echo '<pre>', print_r($content_arrays), '</pre>';
It return a bunch of empty arrays. I just dont get why.

simplexml_load_file returns an object, not an array. So you can't just print it with print_r. You need to do more work to navigate through the SimpleXMLElement.

Related

PHP - json_decode not working when through a curl request

I have a PHP array on the source file - source.php; and I am converting it to a json through $ouput = json_encode($ouput);. The $output variable is already an array.
$ouput = json_encode($ouput);
echo '<pre>';
print_r($ouput);
echo '</pre>';
The json displays well when I call it on my destination file using cURL as a json encoded string. However, when I want to convert the string to PHP array nothing gets displayed.
$displayReturns = curl_exec($curl);
curl_reset($curl);
curl_close($curl);
$displayReturns = json_decode($displayReturns,true);
print_r($displayReturns);
Solved: My source file had the following:
echo '<pre>';
print_r $ouput;
echo '<pre>';
Once I removed them everything worked fine.

Json deserialization return null

I'm trying to deserialize this json.
Actual I stay using the simple html dom library for get the web content, so the next step that I do is using the json_decode() function. But when I'll print the value returned by the function I get NULL. This is the code:
<?php
require_once("simplehtmldom_1_5/simple_html_dom.php");
$html = file_get_html('http://it.soccerway.com/a/block_competition_tables?block_id=page_competition_1_block_competition_tables_8&callback_params=%7B%22season_id%22%3A11663%2C%22round_id%22%3A31554%2C%22outgroup%22%3Afalse%7D&action=changeTable&params=%7B%22type%22%3A%22competition_league_table%22%7D');
$decoded = json_decode($html,true);
var_dump($decoded);
?>
What's wrong in my code? Maybe this isn't the best way for doing this? Hint me.
It seems that your file_get_html function is not working properly, you can get the content of a web with file_get_contents
<?php
$html = file_get_contents('http://it.soccerway.com/a/block_competition_tables?block_id=page_competition_1_block_competition_tables_8&callback_params=%7B%22season_id%22%3A11663%2C%22round_id%22%3A31554%2C%22outgroup%22%3Afalse%7D&action=changeTable&params=%7B%22type%22%3A%22competition_league_table%22%7D');
$decoded = json_decode($html,true);
var_dump($decoded);
?>

Simple html dom - other result than expected

I try to retrieve info from a webpage using simple_html_dom, like this:
<?PHP
include_once('dom/simple_html_dom.php');
$urlpart="http://w2.brreg.no/motorvogn/";
$url = "http://w2.brreg.no/motorvogn/heftelser_motorvogn.jsp?regnr=BR15597";
$html = file_get_html($url);
foreach($html->find('a') as $element)
if(preg_match('*dagb*',$element)) {
$result=$urlpart.$element->href;
$resultcontent=file_get_contents($result);
echo $resultcontent;
}
?>
The $result variable first gives me this URL:
http://w2.brreg.no/motorvogn/dagbokutskrift.jsp?dgbnr=2011365320&embnr=0&regnr=BR15597
When accessing the above URL with my browser, i get the content i expect.
When retrieving the content with $resultcontent, i get a different result, where it says in norwegian "Invalid input".
Any ideas why?
foreach($html->find('a') as $element)
if(preg_match('*dagb*',$element)) {
$result=$urlpart.$element->href;
$resultcontent=file_get_contents(html_entity_decode($result));
echo $resultcontent;
}
This should do the trick.
The problem is with your URL query parameter.
http://w2.brreg.no/motorvogn/dagbokutskrift.jsp?dgbnr=2011365320&embnr=0&regnr=BR15597
The string '&reg' in the URL will be converted to Symbol ® in file_get_contents function which stops you from getting the actual result.
You can use html_entity_decode function in line #11
$resultcontent=file_get_contents(html_entity_decode($result));

getting xml from URL into variable

I am trying to get an xml feed from a url
http://api.eve-central.com/api/marketstat?typeid=1230&regionlimit=10000002
but seem to be failing miserably. I have tried
new SimpleXMLElement,
file_get_contents and
http_get
Yet none of these seem to echo a nice XML feed when I either echo or print_r. The end goal is to eventually parse this data but getting it into a variable would sure be a nice start.
I have attached my code below. This is contained within a loop and $typeID does in fact give the correct ID as seen above
$url = 'http://api.eve-central.com/api/marketstat?typeid='.$typeID.'&regionlimit=10000002';
echo $url."<br />";
$xml = new SimpleXMLElement($url);
print_r($xml);
I should state that the other strange thing I am seeing is that when I echo $url, i get
http://api.eve-central.com/api/marketstat?typeid=1230®ionlimit=10000002
the &reg is the registered trademark symbol. I am unsure if this is "feature" in my browser, or a "feature" in my code
Try the following:
<?php
$typeID = 1230;
// set feed URL
$url = 'http://api.eve-central.com/api/marketstat?typeid='.$typeID.'&regionlimit=10000002';
echo $url."<br />";
// read feed into SimpleXML object
$sxml = simplexml_load_file($url);
// then you can do
var_dump($sxml);
// And now you'll be able to call `$sxml->marketstat->type->buy->volume` as well as other properties.
echo $sxml->marketstat->type->buy->volume;
// And if you want to fetch multiple IDs:
foreach($sxml->marketstat->type as $type){
echo $type->buy->volume . "<br>";
}
?>
You need to fetch the data from the URL in order to make an XML object.
$url = 'http://api.eve-central.com/api/marketstat?typeid='.$typeID.'&regionlimit=10000002';
$xml = new SimpleXMLElement(file_get_contents($url));
// pre tags to format nicely
echo '<pre>';
print_r($xml);
echo '</pre>';

Retrieving info from JSON array with PHP

i have this public JSON https://raw.github.com/currencybot/open-exchange-rates/master/latest.json and i need to extract data from it, right now i tried something like this:
$input = file_get_contents("https://raw.github.com/currencybot/open-exchange-rates/master/latest.json");
$json = json_decode($input);
echo $json[rates]->EUR;
but i obtain a blank page, any suggestion? Thanks !! Ste
json_decode either returns an object, or an array (when you use the second param). You are trying to use both.
Try:
$json = json_decode($input);
echo $json->rates->EUR;
OR
$json = json_decode($input, true);
echo $json['rates']['EUR'];
As to the blank page, please add the following to the top of your script:
error_reporting(E_ALL);
init_set('display_errors', true);
A 500 Error indicates you are unable to resolve that url using file_get_contents.
Check here for more information.
Read the help for json_decode ... without the second parameter it returns and object not an array ...
Either :
$json = json_decode($input);
echo $json->rates->EUR;
or
$json = json_decode($input,true);
echo $json['rates']['EUR'];
Try:
$arr = json_decode($input, true);
var_dump($arr);
echo $arr['rates']['EUR'];
Further Reading:
http://de.php.net/manual/de/function.json-encode.php
For anexample with cURL and SSL read this:
http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
The link looks like starting with https, while file_get_contents can't deal with SSL. My suggestion is using curl.

Categories