parse xml to mysql - php

I have a XML feed from which I need to parse some of it and store it into MySQL table, but I'm facing a new kind of XML (I'm not good at this)
Here is the XML I'm getting which i'm opening using simplexml_load_file from this address:
It's a pubmed journal publication I need to store authorlist and some other, but with a hint on how to access it I can manage.
Thanks a lot
SimpleXMLElement Object
(
[DocSum] => SimpleXMLElement Object
(
[Id] => 22064119
[Item] => Array
(
[0] => 2011 Dec
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => EPubDate
[Type] => Date
)
)
[2] => Clin Nucl Med
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => AuthorList
[Type] => List
)
[Item] => Array
(
[0] => Jackson TA
[1] => Choong KW
[2] => Eng JA
[3] => McAneny D
[4] => Subramaniam RM
[5] => Knapp PE
)
)
[4] => Knapp PE
[5] => F-18 FDG PET/CT Imaging of Endogenous Cushing Syndrome.
[6] => 36
[7] => 12
[8] => e231-2
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => LangList
[Type] => List
)
[Item] => English
)
[10] => 7611109
[11] => 0363-9762
[12] => 1536-0229
[13] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => PubTypeList
[Type] => List
)
[Item] => Journal Article
)
[14] => PubMed - in process
[15] => ppublish
[16] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => ArticleIds
[Type] => List
)
[Item] => Array
(
[0] => 10.1097/RLU.0b013e3182336360
[1] => 00003072-201112000-00044
[2] => 22064119
[3] => 22064119
[4] => 22064119
)
)
[17] => 10.1097/RLU.0b013e3182336360
[18] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => History
[Type] => List
)
[Item] => Array
(
[0] => 2011/11/09 06:00
[1] => 2011/11/09 06:00
[2] => 2011/11/09 06:00
)
)
[19] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => References
[Type] => List
)
)
[20] => 1
[21] => 0
[22] => Clinical nuclear medicine
[23] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Name] => ELocationID
[Type] => String
)
)
[24] => 2011 Dec;36(12):e231-2
)
)
)

You can run an XPath on the object like this: $xml->xpath('/DocSum/Item/3/Item');

Related

How to iterate and loop through this XML response with cUrl and PHP?

I need help looping through a complex XML response I receive from making an api call using PHP cURL. I think I've tried everything I could find here on SO and in the PHP docs. Nothing works. I need help understanding how to do this. For instance if I wanted to print the admin address, how would I do this?
PHP cURL
$ch = curl_init($connection_details['api_host_port']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$response = curl_exec($ch);
//
//
//
//parse xml string into SimpleXML objects
$xml = new SimpleXMLElement($response);
print_r($xml);
Full Response
SimpleXMLElement Object
(
[header] => SimpleXMLElement Object
(
[version] => 0.9
)
[body] => SimpleXMLElement Object
(
[data_block] => SimpleXMLElement Object
(
[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => attributes
)
[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => affiliate_id
)
)
[1] => 1
[2] => 2020-08-24 21:55:12
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => nameserver_list
)
[dt_array] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => 0
)
[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => ns1.systemdns.com
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => ipaddress
)
)
[2] => 1
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => 1
)
[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => ipaddress
)
)
[1] => 2
[2] => ns2.systemdns.com
)
)
)
)
)
)
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => tld_data
)
)
[5] => 2021-08-24 21:55:10
[6] => NONE
[7] => 2021-08-24 21:55:10
[8] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => contact_set
)
[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => owner
)
[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => +1.5556667777
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => address2
)
)
[2] => Miami
[3] => John
[4] => NA
[5] => FL
[6] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => address3
)
)
[7] => active
[8] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => fax
)
)
[9] => 3200 Northwest 67th Avenue
[10] => Doe
[11] => tech#example.com
[12] => US
[13] => 33122
)
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => admin
)
[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => Miami
[1] => John
[2] => FL
[3] => NA
[4] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => address3
)
)
[5] => active
[6] => 3200 Northwest 67th Avenue
[7] => Doe
[8] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => fax
)
)
[9] => tech#example.com
[10] => US
[11] => 33122
[12] => +1.5556667777
[13] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => address2
)
)
)
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => billing
)
[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => +1.5556667777
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => address2
)
)
[2] => FL
[3] => NA
[4] => Miami
[5] => John
[6] => tech#example.com
[7] => 33122
[8] => US
[9] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => address3
)
)
[10] => 3200 Northwest 67th Avenue
[11] => active
[12] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => fax
)
)
[13] => Doe
)
)
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => tech
)
[dt_assoc] => SimpleXMLElement Object
(
[item] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => address2
)
)
[1] => +1.5556667777
[2] => Jane Doe
[3] => Los Angeles
[4] => CA
[5] => Company Name
[6] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => fax
)
)
[7] => active
[8] => 555 My Street
[9] => Jane Doe
[10] => SimpleXMLElement Object
(
[#attributes] => Array
(
[key] => address3
)
)
[11] => US
[12] => 77788
[13] => tech#example.com
)
)
)
)
)
)
[9] => 1
[10] => 2020-08-24 21:55:10
[11] => 0
)
)
)
[1] => DOMAIN
[2] => 200
[3] => XCP
[4] => REPLY
[5] => Query Successful
[6] => 1
)
)
)
)
)
per request EDIT WITH $xml->asXML():
0.9
Query Successful
200
1
XCP
DOMAIN
REPLY
1
John
NA
US
+1.5556667777
Doe
3200 Northwest 67th Avenue
Miami
active
tech#example.com
33122
FL
active
Los Angeles
tech#example.com
77788
CA
Company Name
Jane Doe
+1.5556667777
US
555 My Street
Jane Doe
+1.5556667777
US
NA
John
Doe
3200 Northwest 67th Avenue
tech#example.com
Miami
active
33122
FL
33122
active
Miami
tech#example.com
FL
John
NA
US
+1.5556667777
Doe
3200 Northwest 67th Avenue
ns1.systemdns.com
1
2
ns2.systemdns.com
2020-08-24 21:55:12
2021-08-24 21:55:10
NONE
2020-08-24 21:55:10
0
1
2021-08-24 21:55:10
Using the very last example in the link you provide in your comment, something like the below - using xpath - should get you the admin info:
$admin = $xml->xpath('//item[#key="admin"]//item');
foreach($admin as $item) {
echo $item;
}
Output:
US Admin Example Inc. +1.4165550123x1812 CA Suite 100 active Adams adams#example.com Santa Clara 90210 +1.4165550125 32 Oak Street Adler
Or to get server info for the server w/ id 2:
$server = $xml->xpath('//item[#key="nameserver_list"]//item[#key="2"]//dt_assoc//item');
foreach($server as $item) {
echo $item;
}
Output:
24.22.23.28 3 patrick.example.com
etc.

Read complex array from php

I get this array in response from webservice, so How do I read it in a foreach cycle? or some easy way to read it. The [group] they are more than 12 [id]
there is.
Array
( [response] => Array (
[single] => Array ( [parameters] => Array ( [0] => Array ( [name] => msgCode [value] => 0101 )
[1] => Array ( [name] => msgDesc [value] => OK )
[2] => Array ( [name] => status [value] => 1)
[3] => Array ( [name] => message [value] => Normal) ) )
[group] => Array ( [id] => N4BD767 [parameters] => Array ( [0] => Array ( [name] => idFee [value] => 000 ) echo
[1] => Array ( [name] => typeFee [value] => Cuota)
[2] => Array ( [name] => entryDate [value] => 2014-12-17T14:06:47-03:00 )
[3] => Array ( [name] => expirationDate [value] => 2015-12-05T00:00:00-03:00)
[4] => Array ( [name] => amountOrigin [value] => 221980)
[5] => Array ( [name] => surcharges [value] => 1856)
[6] => Array ( [name] => entity [value] => ONLINE)
[7] => Array ( [name] => feeStatus [value] => inicial )
[8] => Array ( [name] => tranNumber [value] => 27) ) ) ) )
Okay guys, this is what I did and it work fine. With no foreach.
$output = array();
array_walk_recursive($result, function($item,$key) use (&$output){
array_push($output,$key,$item);
});
echo var_dump($output);

Likes on Facebook Posts Pagination

I want to count the number of likes (and comments) on a Facebook post. But when there's more than 25 likes on a post, the results are paginated like that:
stdClass Object ( [data] => Array ( [0] => stdClass Object ( [id] => 802020049912534 [name] => Sagar Lama ) [1] => stdClass Object ( [id] => 590020061101130 [name] => Sheguey Squaad ) [2] => stdClass Object ( [id] => 696207733846316 [name] => Ossama Barcalona ) [3] => stdClass Object ( [id] => 1901218896769448 [name] => Gab Alviola ) [4] => stdClass Object ( [id] => 407069606150738 [name] => Thongloy Thichanon ) [5] => stdClass Object ( [id] => 947084195347741 [name] => Thomas Vujić ) [6] => stdClass Object ( [id] => 1614068872196230 [name] => เพื่อนà¸à¸¹à¹€à¸ˆà¹‡à¸š ชีวิตมึงจบ ) [7] => stdClass Object ( [id] => 1431328280529413 [name] => Ali-Bilal Dernek ) [8] => stdClass Object ( [id] => 855095404568872 [name] => Henry Feng ) [9] => stdClass Object ( [id] => 937460179628960 [name] => Rex Lu ) [10] => stdClass Object ( [id] => 1627484497506023 [name] => Röbërt GärÄïâ ) [11] => stdClass Object ( [id] => 971640236220159 [name] => Reyjoel Jimeno ) [12] => stdClass Object ( [id] => 1644471585776456 [name] => Og Catwalk ) [13] => stdClass Object ( [id] => 1652037021731712 [name] => Rafik Benchibane ) [14] => stdClass Object ( [id] => 499561140196412 [name] => Lizy Smith ) [15] => stdClass Object ( [id] => 10207169299621544 [name] => Ngai Lam ) [16] => stdClass Object ( [id] => 505507699598850 [name] => Ëlloï Gonzalez Sanchez ) [17] => stdClass Object ( [id] => 802078276575586 [name] => Armando Sanchez ) [18] => stdClass Object ( [id] => 10205977306769395 [name] => Fredy Santizo ) [19] => stdClass Object ( [id] => 811409772291367 [name] => Malik Davis ) [20] => stdClass Object ( [id] => 842376069172521 [name] => Isak Renee Martinez ) [21] => stdClass Object ( [id] => 915675738506394 [name] => Kimora Stephens Davis ) [22] => stdClass Object ( [id] => 916531748393520 [name] => Isabel Averill ) [23] => stdClass Object ( [id] => 941049645918044 [name] => Mathias Tenezaca Mendez ) [24] => stdClass Object ( [id] => 1452189318420627 [name] => Andrew Hernandez ) ) [paging] => stdClass Object ( [cursors] => stdClass Object ( [after] => MTQ1MjE4OTMxODQyMDYyNw== [before] => ODAyMDIwMDQ5OTEyNTM0 ) [next] => https://graph.facebook.com/v2.3/15087023444_10153196470723445/likes?access_token=xxxxxxxxxxxxxxxxxxx&limit=25&after=MTQ1MjE4OTMxODQyMDYyNw%3D%3D ) )
How can I count likes and comments on a given post without asking the Facebook API too much. I'm using the PHP SDK
You can either use paging to go through all Likes, or you can use the summary parameter to get a total count:
/post-id?fields=likes.summary(true)
Part of the response:
"summary": {
"total_count": 393,
"can_like": true,
"has_liked": false
}

Loop through xml array

Below is my code and I want to extract information of[field] => Array of index [1].It means [1] => 1030.670044.
This is xml file,and I want to store in my database,but unable to loop this in php.
Please Help me.
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 1.0
)
[meta] => SimpleXMLElement Object
(
[type] => resource-list
)
[resources] => SimpleXMLElement Object
(
[#attributes] => Array
(
[start] => 0
[count] => 168
)
[resource] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[classname] => Quote
)
[field] => Array
(
[0] => USD/KRW
[1] => 1030.670044
[2] => KRW=X
[3] => 1398752590
[4] => currency
[5] => 2014-04-29T06:23:10+0000
[6] => 0
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[classname] => Quote
)
[field] => Array
(
[0] => SILVER 1 OZ 999 NY
[1] => 0.051059
[2] => XAG=X
[3] => 1398721323
[4] => currency
[5] => 2014-04-28T21:42:03+0000
[6] => 400
)
)
This is a SimpleXML object. If the variable is called $xml you can access the field[1] like this:
foreach ($xml->resources->resource as $res) {
var_dump( $res->field[1] );
}

PHP Facebook API - Access Object Array Item

I have an object array (shown at the bottom) and can access the data by doing this:
echo $MyVariable->id;
echo $MyVariable->type;
and so on. However, I can't access the item [count] by doing this:
echo $MyVariable->id;
Anybody know why? Count seems to be used for something else, a count perhaps?
stdClass Object (
[id] => 573948779291487
[from] => stdClass Object (
[category] => Bar
[category_list] => Array (
[0] => stdClass Object (
[id] => 218693881483234
[name] => Pub
)
[1] => stdClass Object (
[id] => 164049010316507
[name] => Gastropub
) )
[name] => The Melbourne Arms
[id] => 533195070033525
)
[name] => The Melbourne Arms Gallery
[location] => The Melbourne Arms
[place] => stdClass Object (
[id] => 533195070033525
[name] => The Melbourne Arms
[location] => stdClass Object (
[street] => Main Street
[city] => Melbourne
[country] => United Kingdom
[zip] => YO42 4QJ
[latitude] => 53.8872441
[longitude] => -0.8554097
) )
[link] => http://www.facebook.com/album.php?fbid=573948779291487&id=533195070033525&aid=1073741825
[cover_photo] => 573948812624817
[count] => 28
[type] => normal
[created_time] => 2013-03-13T22:22:45+0000
[updated_time] => 2013-03-13T22:34:40+0000
[can_upload] =>
[likes] => stdClass Object (
[data] => Array (
[0] => stdClass Object (
[id] => 1361671776
[name] => Lesley Maiden
)
[1] => stdClass Object (
[id] => 518580863
[name] => Diane Maiden
)
[2] => stdClass Object (
[id] => 1254875611
[name] => Vanessa Wilson
)
[3] => stdClass Object (
[id] => 533195070033525
[name] => The Melbourne Arms
)
[4] => stdClass Object (
[id] => 100001664649752
[name] => Bowen Lee
) )
[paging] => stdClass Object (
[next] => https://graph.facebook.com/573948779291487/likes?limit=25&offset=25&__after_id=100001664649752
) ) ) 1
Try handling your data differently
$data = file_get_contents("https://graph.facebook.com/573948779291487");
$data = json_decode($data, true);
echo $data['count'];

Categories