unable to parse xml data - php

i am unable to parse xml document .
well my task was like that
i got xml page from curl which contain ip info
<ip_address>209.59.194.20</ip_address><ip_type>Mapped</ip_type><Network><organization>thoughtconvergence.com</organization><carrier>whidbey internet services</carrier><asn>6295</asn><connection_type/><line_speed/><ip_routing_type>fixed</ip_routing_type><Domain><tld>com</tld><sld>trafficz</sld></Domain></Network><Location><continent>north america</continent><latitude>34.03708</latitude><longitude>-118.42789</longitude><CountryData><country>united states</country><country_code>us</country_code><country_cf>99</country_cf></CountryData><region>southwest</region><StateData><state>california</state><state_code>ca</state_code><state_cf>80</state_cf></StateData><dma>803</dma><msa>31100</msa><CityData><city>los angeles</city><postal_code>90064</postal_code><time_zone>-8</time_zone><area_code>323</area_code><city_cf>61</city_cf></CityData></Location></ipinfo>
i try to parse it
$book = simplexml_load_string($datax);
$ipadd = $book->ip_address;
$ipatype = $book->ip_type;
$ip_routing_type = $book->Network->ip_routing_type;
$state = $book->Location->StateData->state;
$country = $book->Location->CountryData->country;
$continent = $book->Location->continent;
$region = $book->Location->region;
now am getting few errors
1) Entity: line 2: parser error : Extra content at the end of the document
2 Trying to get property of non-object in line #

Your XML is invalid. It has a closing tag for a root element at the end of the data
</ipinfo>
but there is no header tag. If you tack:
<ipinfo>
to the front of it I'll bet it will work.

Related

How to use Nominatim API through PHP to retrieve latitude and longitude?

Below is the code that I am currently using in which I pass an address to the function and the Nominatim API should return a JSON from which I could retrieve the latitude and longitude of the address from.
function geocode($address){
// url encode the address
$address = urlencode($address);
$url = 'http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q={$address}&format=json&limit=1';
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
// get the important data
$lati = $resp['lat'];
$longi = $resp['lon'];
// put the data in the array
$data_arr = array();
array_push(
$data_arr,
$lati,
$longi
);
return $data_arr;
}
The problem with it is that I always end up with an Internal Server Error. I have checked the Logs and this constantly gets repeated:
[[DATE] America/New_York] PHP Notice: Undefined index: title in [...php] on line [...]
[[DATE] America/New_York] PHP Notice: Undefined variable: area in [...php] on line [...]
What could be the issue here? Is it because of the _ in New_York? I have tried using str_replace to swap that with a + but that doesn't seem to work and the same error is still returned.
Also, the URL works fine since I have tested it out through JavaScript and manually (though {$address} was replaced with an actual address).
Would really appreciate any help with this, thank you!
Edit
This has now been fixed. The problem seems to be with Nominatim not being able to pickup certain values and so returns an error as a result
The errors you have mentioned don't appear to relate to the code you posted given the variables title and area are not present. I can provide some help for the geocode function you posted.
The main issue is that there are single quotes around the $url string - this means that $address is not injected into the string and the requests is for the lat/long of "$address". Using double quotes resolves this issue:
$url = "http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q={$address}&format=json&limit=1";
Secondly, the response contains an array of arrays (if were not for the limit parameter more than one result might be expected). So when fetch the details out of the response, look in $resp[0] rather than just $resp.
// get the important data
$lati = $resp[0]['lat'];
$longi = $resp[0]['lon'];
In full, with some abbreviation of the array building at the end for simplicity:
function geocode($address){
// url encode the address
$address = urlencode($address);
$url = "http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q={$address}&format=json&limit=1";
// get the json response
$resp_json = file_get_contents($url);
// decode the json
$resp = json_decode($resp_json, true);
return array($resp[0]['lat'], $resp[0]['lon']);
}
Once you are happy it works, I'd recommend adding in some error handling for both the http request and decoding/returning of the response.

file_get_html error, not working

I'm using Simple HTML Dom to try scrape a HTML table.
I follow their instructions and have looked at many other code examples, but the file_get_html just doesn't seem to work.
Here is my code:
<?php
// Simple HTML Dom Parser
include('simple_html_dom.php');
//$worlds = ["Amera", "Antica", "Astera", "Aurera", "Aurora", "Bellona", "Belobra", "Beneva", "Calmera", "Calva", "Calvera", "Candia", "Celesta", "Chrona", "Danera", "Dolera", "Efidia", "Eldera", "Ferobra", "Fidera", "Fortera", "Garnera", "Guardia", "Harmonia", "Honera", "Hydera", "Inferna", "Iona", "Irmada", "Julera", "Justera", "Kenora", "Kronera", "Laudera", "Luminera", "Magera", "Menera", "Morta", "Mortera", "Neptera", "Nerana", "Nika", "Olympa", "Osera", "Pacera", "Premia", "Pythera", "Quilia", "Refugia", "Rowana", "Secura", "Serdebra", "Shivera", "Silvera", "Solera", "Tavara", "Thera", "Umera", "Unitera", "Veludera", "Verlana", "Xantera", "Xylana", "Yanara", "Zanera", "Zeluna"];
//foreach ($worlds as $world) {
// All HTML from the online list
$html = file_get_html('https://secure.tibia.com/community/?subtopic=worlds&world=Antica');
// Search for the online list table content
foreach ($html->find('tr[class=Table2]') as $row) {
$name = $row->find('td', 0)->plaintext;
$level = $row->find('td', 1)->plaintext;
$vocation = $row->find('td', 2)->plaintext;
echo $name . ' | ' . $level . ' | ' . $vocation . '<br>';
}
//}
?>
And I get these errors:
Warning: file_get_contents(): stream does not support seeking in D:\xampp\htdocs\simple_html_dom.php on line 76
Warning: file_get_contents(): Failed to seek to position -1 in the stream in D:\xampp\htdocs\simple_html_dom.php on line 76
Fatal error: Uncaught Error: Call to a member function find() on boolean in D:\xampp\htdocs\index.php:13 Stack trace: #0 {main} thrown in D:\xampp\htdocs\index.php on line 13
What am I doing wrong?
The table I am trying to scrape is the "Players Online" table on:
https://secure.tibia.com/community/?subtopic=worlds&world=Antica
Try this:
$html = str_get_html(file_get_contents($url));
This is a simple_html_dom library problem with the latest versions of PHP.
To correct it, simply change "$offset = -1," to "$offset = 0," in the parameters of the "file_get_html" function in the "simple_html_dom.php" file.
I don't know much about simpledom but i think you might need to use a more robust library like https://github.com/FriendsOfPHP/Goutte

Failed to post a url query to REST web service - issue with my url?

I am submitting a URL request to a RESTful webservice. When I submit a variable in the url e.g "NGF and trkA", I am able to retrieve an XML responses from the remote server. However, when I input "NGF/trkA", my request fails. I really do not understand why as I have used rawurlendcode(). See the code below:
<?php
/*After pdf parsing, the result is contained in $fullText.*/
$fullText="NGF/TrkA*";
/*Remove line breaks.*/
$fullText_clean = str_replace("\n", " ", $fullText);
/*Excise the first 2 sentences of $fullText_clean (or 100 characters).*/
$truncated = substr($fullText_clean,0);
// Properly encode $truncated.
$truncated_encode = rawurlencode($truncated);
// Plug this variable, $truncated_encode, into the search url.
$xml = 'http://www.ebi.ac.uk/europepmc/webservices/rest/search/query='.$truncated_encode.'&resulttype=core';
// Return SimpleXMLElement object
$xmlObject = simplexml_load_file($xml);
if($xmlObject){
//Return information within the desired tag(s).
$title = $xmlObject->resultList->result[0]->title;
echo $fullText."<br />".
$fullText_clean."<br />".
$truncated."<br />".
$truncated_encode."<br />".
$title;
/*echo $title."<br />".$truncated;*/
}else{
echo "Failed to retreive SimpleXMLElement object.";
}
?>
This is the error that I get when $fullText="NGF/trkA":
Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://www.ebi.ac.uk/europepmc/webservices/rest/search/query=NGF%2FTrkA%2A&result type=core" in /Applications/XAMPP/xamppfiles/htdocs/Lifescieco_pdfUpload/puttingitTogether.php on line 21
Failed to retreive SimpleXMLElement object.

Parsing HTML Table - PHP

I am trying to parse many HTML tables, with the URLs stored in the database. The current problem with my code is that it will fail on a different table every time. Here is the part of the code that gets the error:
while ($sqlrow = mysqli_fetch_row($res)) {
echo "Started Processing Table " . $tables . PHP_EOL;
$tables++;
$data = file_get_contents($sqlrow[1]);
$dom->loadHTML($data);
$dom->preserveWhiteSpace = false;
$teamtable = $dom->getElementById("reTeamTable");
$teamrows = $teamtable->getElementsByTagName('tr');
The lines that usually fails is either the "getElementById" command or the "getElementsByTagName" command. The error I am getting is: "PHP Fatal error: Call to a member function getElementsByTagName() on a non-object in /scouting/teamlist.php on line 20". I don't understand why this is getting an error on a different URL every time.
Its means that $dom doesnt find element with id="reTeamTable" ( $teamtable is null ). Before call getElementsByTagName , check $teamtable on empty.

PHP, FACEBOOK, PAGES, RSS: Help turning page feed into RSS (getting errors)

Ok, so following some instructions I found in another post here on StackOverflow, I have constructed a script to get a fan pages feed and turn it into an RSS2 feed. However, the script required a few changes and Im not the best programmer in the world, so I need a little help.
Im getting this error:
Warning: Invalid argument supplied for foreach() in feed.php on line 48
Im not sure what the invalid argument is all about.
<?
// error reporting
echo '<pre>';
ini_set('display_errors', 'on');
error_reporting(E_ALL);
// require your facebook php sdk
require('./facebook/facebook.php');
// include the feed generator feedwriter file
include("./feed/FeedWriter.php");
// config secret key and appid
$config = array(
'appId' => '',
'secret'=> ''
);
// Initialize
$facebook = new Facebook($config);
// Set Apps Permissions Request
$permission_scope = "";
// get users access token
$access_token = $facebook->getAccessToken();
// get page post
$feed_url = 'https://www.facebook.com/Ritualdubstep/feed?access_token='.$access_token;
$feed_json = file_get_contents($feed_url);
$feed_data = json_decode($feed_json);
// create the feedwriter object
$feed = new FeedWriter(RSS2);
$feed->setTitle('Ritual Dubstep'); // set your feed title
$feed->setLink('https://www.facebook.com/Ritualdubstep'); // set the url to the feed page you're generating
$feed->setChannelElement('updated', date(DATE_RSS , time()));
$feed->setChannelElement('author', array('name'=>'Ritual Dubstep SF')); // set the author name
// iterate through the facebook response to add items to the feed
foreach($feed_data['data'] as $entry){
if(isset($entry["message"])){
$item = $feed->createNewItem();
$item->setTitle($entry["from"]["name"]);
$item->setDate($entry["updated_time"]);
$item->setDescription($entry["message"]);
if(isset($entry["link"]))
$item->setLink(htmlentities($entry["link"]));
$feed->addItem($item);
}
}
// generate feed
$feed->genarateFeed();
?>
Generally it means that the first argument in the foreach call (in this case $feed_data['data']) is not a valid array.
Make sure that $feed_data['data'] exists (isset($feed_data['data'])) and that it is an array (is_array($feed_data['data'])) before running entering the foreach loop.
And - as shapeshifter mentioned in the comments - you might want to start troubleshooting by var_dump($feed_data['data']) right before you start the foreach loop to see what's being generated.

Categories