I have been searching google for over an hour and am frustrated, This seems so simple. All I am trying to print is my accountId. Here is the xml i am being returned from the server:
SimpleXMLElement Object
(
[accounts] => SimpleXMLElement Object
(
[account] => SimpleXMLElement Object
(
[billingStreet] => SimpleXMLElement Object
(
)
[billingCity] => SimpleXMLElement Object
(
)
[billingState] => SimpleXMLElement Object
(
)
[billingPostalCode] => SimpleXMLElement Object
(
)
[billingCountry] => SimpleXMLElement Object
(
)
[city] => Los Angeles
[accountId] => XXXXX
[companyName] => SimpleXMLElement Object
(
)
[country] => United States
[email] => XXXXX
[enabled] => 1
[fax] => SimpleXMLElement Object
(
)
[firstName] => XXXXX
[lastName] => XXXXX
[multiClientFolder] => 0
[multiUser] => 0
[phone] => XXXXX
[postalCode] => XXXXX
[state] => CA
[street] => XXXXX
[title] => SimpleXMLElement Object
(
)
[accountType] => 0
[subscriberLimit] => 250000
)
)
[total] => 1
[limit] => 20
[offset] => 0
)
All i want is accountId. I am using this and it doesnt print anything:
$ch=curl_init("https://app.sandbox.icontact.com/icp/a/");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$buf = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($buf);
$aid=$xml->$accounts->$account->$accountId;
print($aid);
i can print the entire xml array just fine with print_r though. Im not sure what i am doing wrong
Maybe $aid=$xml->accounts->account->accountId;?
If there is the possibility of getting more than one account in the response, you can add an array index to the account element:
$aid = $xml->accounts->account[0]->accountId;
Or iterate over the accounts:
foreach ($xml->accounts->account as $account) {
...
}
Also note that $aid will be of type SimpleXMLElement. In most cases you can use this as-is and it will be automatically cast to the appropriate type, but if you want the value as a string you can use an explicit cast:
$aid = (string) $xml->accounts->account[0]->accountId;
Related
I am trying to fetch videos from youtube using below code:
$youtube_api_url = "https://www.googleapis.com/youtube/v3/search?key=".$new_key->api_key;
$youtube_api_url = $youtube_api_url."&order=relevance&type=video&maxResults=20&part=snippet&videoCategoryId=10&q=". urlencode(preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '',$trackRecord->name));
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$youtube_api_url);
$result=curl_exec($ch);
$response = json_decode($result, true);
echo "<pre>"; print_r($response); die;
It is returning wrong number of results as below:
Array
(
[items] => Array
(
[0] => Array
(
[kind] => youtube#searchResult
[etag] => "m2yskBQFythfE4irbTIeOgYYfBU/ne5AZXpdoLtAn-mlwu4ipioOQ_I"
[id] => Array
(
[kind] => youtube#video
[videoId] => dB0ITzfbwfw
)
[snippet] => Array
(
[publishedAt] => 2017-01-23T17:31:23.000Z
[channelId] => UCdW5nPo1oyD2vLkvoYijmEg
[title] => Lihtz Kamraz "The Switch Up" Interview Part 2
[description] => In 2016, a lot of break out artists emerged and made a mark in the game. As 2017 begins, one name that you should definitely be on the lookout for is Lihtz ...
[thumbnails] => Array
(
[default] => Array
(
[url] => https://i.ytimg.com/vi/dB0ITzfbwfw/default.jpg
[width] => 120
[height] => 90
)
[medium] => Array
(
[url] => https://i.ytimg.com/vi/dB0ITzfbwfw/mqdefault.jpg
[width] => 320
[height] => 180
)
[high] => Array
(
[url] => https://i.ytimg.com/vi/dB0ITzfbwfw/hqdefault.jpg
[width] => 480
[height] => 360
)
)
[channelTitle] => HIPHOPSINCE1987TV
[liveBroadcastContent] => none
)
)
)
)
As you can see it is showing total and per page result as:
[pageInfo] => Array
(
[totalResults] => 14
[resultsPerPage] => 20
)
How can I get all results?
I am using php curl to get account and transaction data from the plaid api. I'd like to see it in a nice format so I can go about creating loops to save the information into a database. The decoded json output, when printed with print_r, is returned like this:
array ( [accounts] => Array ( [0] => Array ( [_id] => QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK [_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA [_user] => eJXpMzpR65FP4RYno6rzuA7OZjd9n3Hna0RYa [balance] => Array ( [available] => 1203.42 [current] => 1274.93 ) [institution_type] => fake_institution [meta] => Array ( [name] => Plaid Savings [number] => 9606 ) [subtype] => savings [type] => depository ) [1] => Array ( [_id] => nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy [_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA...
I'd like to get it looking like this:
Array (
[accounts] => Array (
[0] => Array (
[_id] => QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK
[_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA
[_user] => eJXpMzpR65FP4RYno6rzuA7OZjd9n3Hna0RYa
[balance] => Array (
[available] => 1203.42
[current] => 1274.93 )
[institution_type] => fake_institution
[meta] => Array (
[name] => Plaid Savings
[number] => 9606 )
[subtype] => savings
[type] => depository )
[1] => Array (
[_id] => nban4wnPKEtnmEpaKzbYFYQvA7D7pnCaeDBMy
[_item] => KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA
`
For reference, here is the code to get the response:
$data = array(
"client_id"=>"test_id",
"secret"=>"test_secret",
"access_token"=>"test_chase"
);
$string = http_build_query($data);
//initialize session
$ch=curl_init("https://tartan.plaid.com/connect/get");
//set options
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute session
$accountData = json_decode(curl_exec($ch), true);
print_r($accountData);
//close session
curl_close($ch);
Any thoughts other than loops?
php code:
<?php
$url = 'https://www.rudolphs-christmasshop.com.au/api/v2/products/';
$username ='xyz'; $password ='ca25fe6947564b9479sdfsaffsaffasfasfsaffdasfe5866b4';
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD,$username . ':' . $password);
$result = curl_exec($ch); curl_close($ch);
$xml = simplexml_load_string($result);
?>
output
SimpleXMLElement Object (
[product] => Array (
[0] => SimpleXMLElement Object (
[id] => 29
[name] => SimpleXMLElement Object ( )
[type] => physical
[sku] => 22254
[description] => SimpleXMLElement Object ( )
[search_keywords] => SimpleXMLElement Object ( )
[availability_description] => SimpleXMLElement Object ( )
[price] => 22.9500
[inventory_warning_level] => 5
[warranty] => SimpleXMLElement Object ( )
[weight] => 0.2500
[width] => 13.0000
[height] => 11.0000
[depth] => 8.0000
[view_count] => 125
[page_title] => Aussie Koala and Baby Christmas Ornament - Australiana
[meta_keywords] => koala bear decoration, koala christmas ornament, australian decorations, aussie christmas, christmas decoration
[meta_description] => SimpleXMLElement Object ( )
[layout_file] => product.html
[is_price_hidden] => false
[price_hidden_label] => SimpleXMLElement Object ( )
[categories] => SimpleXMLElement Object (
[value] => 30
)
[downloads] => SimpleXMLElement Object (
[link] => /products/29/downloads )
[images] => SimpleXMLElement Object (
[link] => /products/29/images
)
)
)
)
)
How could I get the image url and display image on browser
May be wrong but I guess like this if I look here
echo $xml->product[0]->downloads->images->link;
But if you show us your XML we are more able to help you.
Greets
I can't test it here. But you can access the tree with:
if you have more products:
<?php
foreach($xml->product as $pout ) {
echo $pout->downloads->images->link;
}
?>
if you want only one product something like this:
$xml->product[0]->downloads->images->link;
I'm trying to output the data from an XML file datafeed, the support documents are pretty poor and I'm having trouble looping through the XML.
SimpleXMLElement Object (
[#attributes] => Array (
[version] => 6 [type] => WEB [date] => 20120220 )
[account] => SimpleXMLElement Object (
[#attributes] => Array (
[code] => XXXX
)
[sites] => SimpleXMLElement Object (
[site] => SimpleXMLElement Object (
[#attributes] => Array (
[code] => XXXX
)
[name] => Name [address1] => Address 1 [address2] => town [address3] => UK [county] => County [postcode] => Postcode [phone] => 0123456789 [fax] => 0123456789
[vehicles] => SimpleXMLElement Object (
[vehicle] => Array (
[0] => SimpleXMLElement Object (
[#attributes] => Array (
[code] => 5956 [new] => N
[engineSize] => 998
[mileage] => 8000
[type] => MC
[reg] => XXX
[regDate] => 20070414
[created] => 20110928090130
[modified] => 20110928090130
) [manufacturer] => Honda
[description] => CBR 1000 RR-7
[colour] => Black
[body] => Motorcycle
[fuel] => Petrol
[status] => In Stock
)
[1] => SimpleXMLElement Object (
[#attributes] => Array (
[code] => 5958
[new] => Y
[engineSize] => 125
[type] => MC
[created] => 20110930090254
[modified] => 20110930090254
)
[manufacturer] => Honda
[description] => WW125EX2A
[colour] => BLACK(NHA35)
[body] => SCOOTER
[status] => In Stock
[category] => SCOOTER
)
)
)
)
)
)
The data is loaded by:
$request =
"<"."?xml version='1.0' encoding='iso-8859-1'?".">".
"<download account='XXXX' password='XXXX' version='6' request='EXP' format='XML' dealer='XXXX' vehicles='Y' />";
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml", "Content-Length: " . strlen($request)));
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'https://www.catalyst-findit.co.uk/download.php');
$response = curl_exec($ch);
$xml = simplexml_load_string($response);
print_r($xml);
I'm mainly interested in the vehicle data, which I have tried to load by:
echo (string) $xml->vehicles->vehicle[0];
Although this doesn't seem to work.
Be grateful if anybody could offer any advice :)
Thanks in advance.
Edit XML file (displaying only two vehicles for easy reading):
<?xml version="1.0" encoding="iso-8859-1"?>
<findit xmlns="http://www.catalyst-findit.co.uk/download" version="6" type="WEB" date="20120220">
<account code="XXX">
<sites>
<site code="BRI">
<name>XXX</name>
<address1>XXX</address1>
<address2>XXX</address2>
<address3>XXX</address3>
<county>XXX</county>
<postcode>XXX</postcode>
<phone>01472 123456</phone>
<fax>01472 123456</fax>
<vehicles>
<vehicle code="XXX" new="N" engineSize="998" mileage="8000" type="MC" reg="XXX" regDate="20070414" created="20110928090130" modified="20110928090130">
<manufacturer>Honda</manufacturer>
<description>CBR 1000 RR-7</description>
<colour>Black</colour>
<body>Motorcycle</body>
<fuel>Petrol</fuel>
<status>In Stock</status>
</vehicle>
<vehicle code="XXX" new="Y" engineSize="125" type="MC" created="20110930090254" modified="20110930090254">
<manufacturer>Honda</manufacturer>
<description>XXX</description>
<colour>BLACK(NHA35)</colour>
<body>SCOOTER</body>
<status>In Stock</status>
<category>SCOOTER</category>
</vehicle>
</vehicles>
</site>
</sites>
</account>
</findit>
Use XPath to search for specific node data.
$text = <<<EOF
... XML data ...
EOF;
$xml = new SimpleXMLElement($text);
# register xpath prefix for default namespace
$namespaces = $xml->getDocNamespaces();
$xml->registerXPathNamespace('__ns', $namespaces['']);
# get first vehicle data
$result = $xml->xpath('//__ns:vehicles/__ns:vehicle[1]');
# get all vehicles in array
$vehicles = $xml->xpath('//__ns:vehicles/__ns:vehicle');
print_r($result); # or
echo($result[0]->colour);
foreach ($vehicles as $v) {
echo ($v->colour . "\n");
}
I'm trying to read a rss feed and store its value in my database . for this I'm using this
$homepage = file_get_contents('http://showmycode.co.in/supermob/feeds/rss/Jaipur');
$movies = new SimpleXMLElement($homepage);
echo '<pre>';
print_r($movies);
and then I found
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 2.00
)
[channel] => SimpleXMLElement Object
(
[title] => supermobdeal
[description] => best discount accross canada
[link] => SimpleXMLElement Object
(
)
[lastBuildDate] => Thu, Nov 3rd 2011, 00:32 GMT
[generator] => supermobdeal
[image] => SimpleXMLElement Object
(
[url] => http://showmycode.co.in/supermob/app/webroot/img/site_logo.png
[title] => supermobdeal
[link] => http://showmycode.co.in/supermob/app/webroot/img/site_logo.png
[description] => Feed provided by supermobdeal
)
[item] => Array
(
[0] => SimpleXMLElement Object
(
[title] => Let your
[link] => http://showmycode.co.in/supermob/homes/index/Jaipur/25
[small_image_url] => http://showmycode.co.in/supermob/app/webroot/img/uploads/product_images/small/
[description] => trainers r any course
[start_date] => 2011-10-26
[end_date] => 2011-11-30
[start_time] => 09:26:00
[end_time] => 09:26:50
[price] => 50$
[value] => 199$
[discount] => 74.87%
[city] => Jaipur
)
[1] => SimpleXMLElement Object
(
[title] => Feel a
[link] => http://showmycode.co.in/supermob/homes/index/Jaipur/31
[small_image_url] => http://showmycode.co.in/supermob/app/webroot/img/uploads/product_images/small/3b8d0_Jaip_OrientSpa_24thOct_M_1.jpg
[description] => Orient Spa
[city] => Jaipur
)
[2] => SimpleXMLElement Object
(
[title] => Feel a
[link] => http://showmycode.co.in/supermob/homes/index/Jaipur/26
[small_image_url] => http://showmycode.co.in/supermob/app/webroot/img/uploads/product_images/small/0a646_Bombay-Biryani1.jpg
[description] => Taxes
[city] => Jaipur
)
)
)
)
how can I store tittle , link or other attributes in my database and store image in folder
//get contanints from SimpleXMLElement Object
foreach($movies->channel->item as $opt){
$title= $opt->title;
$tittle=mysql_real_escape_string($title); //for Escapes special characters in a string you can use for all varriables if require
$link=$opt->link;
$des=$opt->description;
$image=$opt->medium_image_url;
//for store image from http url to my folder
$ch = curl_init($image);
$fp = fopen('images/'. time () .'.'.'jpg', 'wb');//path for store image
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
include('db.php');//config file for db connection
$sql="INSERT INTO rss (tittle, link, image,description,city)
VALUES ('$tittle', '$link', '$image','$des','$city')";//insert record in database table
$result=mysql_query($sql) or die('Error, insert query failed');
this code store varriables value in your database and image in folder from http url.thanks..