954,604 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

how to parse xml data with php

<?php

// initiate curl and set options
$ipin = '69.72.129.3';
$ch = curl_init();
$ver = 'v1/';
$method = 'ipinfo/';
$apikey = '100.hva7qa7893wzcsc3x8dk';  
$secret = 'uHZVvYK5';  
$timestamp = gmdate('U'); // 1200603038
// echo $timestamp;   
$sig = md5($apikey . $secret . $timestamp);
$service = 'http://api.quova.com/';
curl_setopt($ch, CURLOPT_URL, $service . $ver. $method. $ipin . '?apikey=' .
             $apikey . '&sig='.$sig . '&format=xml');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$headers = curl_getinfo($ch);

// close curl
curl_close($ch);

// return XML data
if ($headers['http_code'] != '200') {
   echo "An error has occurred accessing this IP";
  return false;
} else {
   //echo $data;
   return($data);
}

i have neustar code it return ip info in xml format
i want to extract the data and assign it to variables like $ip , $org , $isp etc

so please help am not much famailer with xml i try to simplexml_load_string() but no success

darkc99
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Hi,

I am busy on my school homework right now, but I will attempt to post a solution sometime this weekend if you don't get a response until that time.

Before making my promise a final one, can you please tell us how these information are used?

$method = 'ipinfo/';
$apikey = '100.hva7qa7893wzcsc3x8dk';
$secret = 'uHZVvYK5';


Does the site needs log in?
Do you need to validate?

those questions are really important, because cURL will not work or not allowed to access those info.

Another hint! Did you ever try to use cookiejar on your cURL to make it work?

veedeoo
Posting Pro in Training
439 posts since Oct 2011
Reputation Points: 149
Solved Threads: 60
 

That is the API key for http://api.quova.com/ ($service var): http://developer.quova.com/docs#sample_php

@darkc99 you may want to change and disable this key now, since this is exposed and paid...

cereal
Master Poster
710 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

ya its API Key from quova.com , used to get ip information

well no need to worry about key its free i will get another :D but if i help it will be appreciated

darkc99
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Hi,

I am busy on my school homework right now, but I will attempt to post a solution sometime this weekend if you don't get a response until that time.

Before making my promise a final one, can you please tell us how these information are used?

$method = 'ipinfo/';
$apikey = '100.hva7qa7893wzcsc3x8dk';
$secret = 'uHZVvYK5';

Does the site needs log in? Do you need to validate?

those questions are really important, because cURL will not work or not allowed to access those info.

Another hint! Did you ever try to use cookiejar on your cURL to make it work?

hey when you will post the solution for me :) still waiting

hits ::
just copy above code
run it
its will display data if you see the source code its xml data which is returned from servers
so you have to just parse return xml data ($data) and assign them to separate variables like $ip,$isp,$org etc

darkc99
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

hey when you will post the solution for me :) still waiting

hits :: just copy above code run it its will display data if you see the source code its xml data which is returned from servers so you have to just parse return xml data ($data) and assign them to separate variables like $ip,$isp,$org etc

Nobody here is going to do your work for you and you shouldn't be waiting on people to solve your issue.

Start off here and learn this class well: http://php.net/manual/en/book.dom.php

hag++
Junior Poster
197 posts since Jan 2010
Reputation Points: 34
Solved Threads: 31
 

ya i think have to do myself :(

darkc99
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Check for SimpleXML library: http://www.php.net/manual/en/simplexml.examples-basic.php
You can start with $data:

$a = new SimpleXMLElement($data);
print_r($a);

So you can display the array and display the elements. Bye :)

cereal
Master Poster
710 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

Check for SimpleXML library: http://www.php.net/manual/en/simplexml.examples-basic.php You can start with $data:

$a = new SimpleXMLElement($data);
print_r($a);

So you can display the array and display the elements. Bye :)

it display nothing already i tried to use other xml functions too but it display nothing as output in array.. i do not know why .... :(

here ix xml data which we got from api

<?xml version="1.0" encoding="UTF-8"?>
69.72.129.3Mappedfortressitxfortressitx25653txhighfixednetcyberleonorth america40.83243-74.13984united statesus99mid atlanticnew jerseynj9550135620clifton07014-597380

<?php
$ip_info = new SimpleXMLElement($data);
echo $ip_info->ipinfo->ip_type;
?>

i try to extract the ipinfo from it but failed :(

darkc99
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

You can set an url as first parameter:

<?php
$xml = new SimpleXMLElement('file.xml',false,true); # try also with $data
print_r($xml);
?>

As second paramenter you can set these constants: http://php.net/manual/en/libxml.constants.php
Third paramenter is needed to declare source (in first parameter) as a file and not data strings like something you get from file_get_contents(). Your output should be:

SimpleXMLElement Object
(
    [ip_address] => 69.72.129.3
    [ip_type] => Mapped
    [Network] => SimpleXMLElement Object
        (
            [organization] => fortressitx
            [carrier] => fortressitx
            [asn] => 25653
            [connection_type] => tx
            [line_speed] => high
            [ip_routing_type] => fixed
            [Domain] => SimpleXMLElement Object
                (
                    [tld] => net
                    [sld] => cyberleo
                )

        )

    [Location] => SimpleXMLElement Object
        (
            [continent] => north america
            [latitude] => 40.83243
            [longitude] => -74.13984
            [CountryData] => SimpleXMLElement Object
                (
                    [country] => united states
                    [country_code] => us
                    [country_cf] => 99
                )

            [region] => mid atlantic
            [StateData] => SimpleXMLElement Object
                (
                    [state] => new jersey
                    [state_code] => nj
                    [state_cf] => 95
                )

            [dma] => 501
            [msa] => 35620
            [CityData] => SimpleXMLElement Object
                (
                    [city] => clifton
                    [postal_code] => 07014
                    [time_zone] => -5
                    [area_code] => 973
                    [city_cf] => 80
                )

        )

)

bye.

cereal
Master Poster
710 posts since Aug 2007
Reputation Points: 214
Solved Threads: 120
 

You can set an url as first parameter:

<?php
$xml = new SimpleXMLElement('file.xml',false,true); # try also with $data
print_r($xml);
?>

As second paramenter you can set these constants: http://php.net/manual/en/libxml.constants.php Third paramenter is needed to declare source (in first parameter) as a file and not data strings like something you get from file_get_contents(). Your output should be:

SimpleXMLElement Object
(
    [ip_address] => 69.72.129.3
    [ip_type] => Mapped
    [Network] => SimpleXMLElement Object
        (
            [organization] => fortressitx
            [carrier] => fortressitx
            [asn] => 25653
            [connection_type] => tx
            [line_speed] => high
            [ip_routing_type] => fixed
            [Domain] => SimpleXMLElement Object
                (
                    [tld] => net
                    [sld] => cyberleo
                )

        )

    [Location] => SimpleXMLElement Object
        (
            [continent] => north america
            [latitude] => 40.83243
            [longitude] => -74.13984
            [CountryData] => SimpleXMLElement Object
                (
                    [country] => united states
                    [country_code] => us
                    [country_cf] => 99
                )

            [region] => mid atlantic
            [StateData] => SimpleXMLElement Object
                (
                    [state] => new jersey
                    [state_code] => nj
                    [state_cf] => 95
                )

            [dma] => 501
            [msa] => 35620
            [CityData] => SimpleXMLElement Object
                (
                    [city] => clifton
                    [postal_code] => 07014
                    [time_zone] => -5
                    [area_code] => 973
                    [city_cf] => 80
                )

        )

)

bye.


try also with $data

cant work with $data :(

darkc99
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

well i did it ... problem solved :) .

darkc99
Newbie Poster
24 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: