I try to use amazon API using PHP. If I use
print_r($parsed_xml->Items->Item->ItemAttributes)
it show me some result like
SimpleXMLElement Object (
[Binding] => Electronics
[Brand] => Canon
[DisplaySize] => 2.5
[EAN] => 0013803113662 **
[Feature] => Array (
[0] => High-powered 20x wide-angle optical zoom with Optical Image Stabilizer
[1] => Capture 720p HD movies with stereo sound; HDMI output connector for easy playback on your HDTV
[2] => 2.5-inch Vari-Angle System LCD; improved Smart AUTO intelligently selects from 22 predefined shooting situations
[3] => DIGIC 4 Image Processor; 12.1-megapixel resolution for poster-size, photo-quality prints
[4] => Powered by AA batteries (included); capture images to SD/SDHC memory cards (not included) )**
[FloppyDiskDriveDescription] => None
[FormFactor] => Rotating
[HasRedEyeReduction] => 1
[IsAutographed] => 0
[IsMemorabilia] => 0
[ItemDimensions] => SimpleXMLElement Object (
[Height] => 340
[Length] => 490
[Weight] => 124
[Width] => 350 )
[Label] => Canon
[LensType] => Zoom lens
[ListPrice] => SimpleXMLElement Object (
[Amount] => 60103
[CurrencyCode] => USD
[FormattedPrice] => $601.03 )
[Manufacturer] => Canon
[MaximumFocalLength] => 100
[MaximumResolution] => 12.1
[MinimumFocalLength] => 5
[Model] => SX20IS
[MPN] => SX20IS
[OpticalSensorResolution] => 12.1
[OpticalZoom] => 20
[PackageDimensions] => SimpleXMLElement Object (
[Height] => 460
[Length] => 900
[Weight] => 242
[Width] => 630 )
[PackageQuantity] => 1
[ProductGroup] => Photography
[ProductTypeName] => CAMERA_DIGITAL
[ProductTypeSubcategory] => point-and-shoot
[Publisher] => Canon
[Studio] => Canon
[Title] => Canon PowerShot SX20IS 12.1MP Digital Camera with 20x Wide Angle Optical Image Stabilized Zoom and 2.5-inch Articulating LCD
[UPC] => 013803113662 )
my goal is to get only Feature infomation and I try to use
$feature = $parsed_xml->Items->Item->ItemAttributes->Feature
it does'not work for me because it just show me the first feature only. How do i get all feature information? please help
In your code $feature should contain an array. You can iterate over all this features:
foreach($feature as $f) {
echo $f;
}
Or do you want the features of all items?
Loop through the items and put the Feature attribute in an array?
$feature = array();
foreach($parsed_xml->Items as $item)
{
$feature[] = $item->ItemAttributes->Feature;
}
Looks to me like the feature element is actually an array, you'll just need to iterate through it
// assign the feature array to a var to prevent reading the xml each loop iteration
$features = $parsed_xml->Items->Item->ItemAttributes->Feature
// loop through each feature in the features array
foreach($features as $feature) {
echo "* $feature\n";
}
or if you want to show the index of the feature (which is just 0-n in this case)
// loop through each feature in the features array
foreach($features as $key => $feature) {
echo "$key. $feature\n";
}
Related
So I'm working with an API for vehicles, and I am running into a wall and hoping to get some input from those with more experience with this... I'll start by showing an example of the data I get back from an API call.
Array
(
[make] => Array
(
[id] => 200003644
[name] => Chrysler
[niceName] => chrysler
)
[model] => Array
(
[id] => Chrysler_200
[name] => 200
[niceName] => 200
)
[engine] => Array
(
[equipmentType] => ENGINE
[availability] => USED
[cylinder] => 6
[size] => 3.6
[configuration] => V
[fuelType] => flex-fuel (unleaded/E85)
[horsepower] => 295
[type] => flex-fuel (FFV)
[code] => ERB
[rpm] => Array
(
[horsepower] => 6350
[torque] => 4250
)
[valve] => Array
(
[gear] => double overhead camshaft
)
)
[transmission] => Array
(
[equipmentType] => TRANSMISSION
[availability] => USED
[transmissionType] => AUTOMATIC
)
[drivenWheels] => front wheel drive
[numOfDoors] => 4
[options] => Array
(
[0] => Array
(
[category] => Safety
[options] => Array
(
[0] => Array
(
[id] => 200741607
[name] => SafetyTec
[description] => Adaptive Cruise Control with Stop & Go; Advanced Brake Assist; Automatic high beam control; Blind Spot and Cross Path Detection; Full Speed Forward Collision Warning Plus; Lane Departure Warning Plus; Parallel and Perpendicular Park Assist with Stop; Rain sensitive windshield wipers
[equipmentType] => OPTION
[availability] => All C/All C Platinum
)
)
)
[1] => Array
(
[category] => Package
[options] => Array
(
[0] => Array
(
[id] => 200741480
[name] => Quick Order Package 26N (Fleet)
[description] => Vehicle with standard equipment
[equipmentType] => OPTION
[availability] => All C
)
[1] => Array
(
[id] => 200741610
[name] => Premium Group
[description] => 115V auxiliary power outlet; Exterior mirrors with memory; Heated 2 tone leather steering wheel; Luxury door trim panel; Premium leather trimmed ventilated front seats with leather seat cushion; Radio/driver seat/Climate control with memory; Real wood/bronze chrome interior accents
[equipmentType] => OPTION
[availability] => All C/All C Platinum
)
[2] => Array
(
[id] => 200741715
[name] => Premium Lighting Group
[description] => HID headlamps with LED daytime running lights; LED fog lamps
[equipmentType] => OPTION
[availability] => All S/All C
)
[3] => Array
(
[id] => 200741805
[name] => Navigation And Sound Group I
[description] => 506 watt amplifier; SiriusXM traffic with 5-year of included service; Travel Link Service with 5-year of included service; 9 amplified speakers with subwoofer; GPS navigation; HD radio; Uconnect 8.4AN AM/FM/SiriusXM/Hard disc drive/Bluetooth/Navigation
[equipmentType] => OPTION
[availability] => All C
)
)
)
)
So if I have this data stored in a variable called $data for example, I can do something like this and it works:
foreach ($data['options'] as $options) {
echo $options['category'];
}
That will return me "Safety", and "Package".
However, if I wanted to get the vehicle's make and I do something like this:
foreach ($data['make'] as $make) {
echo $make['name'];
}
It just returns me the value : Cc (Upper case C from the make name, and lowercase c from the make niceName). Why is it doing this? What am I doing wrong?
Thank you!
There's only one 'make', so I think you just want this (no loop):
echo $data['make']['name'];
Every element of $data['make'] is a string (maybe integer), but not an array as every element of $data['options']. So, you can't use [] notation in case of $data['make'].
Just:
foreach ($data['make'] as $make) {
echo $make;
}
will do what you need.
I have all the boards of logged in user and using pinterest api i am able to fetch the pins from given board but when it returns the json there is no image link, the image field return as null. So how to get image with oin content. Here is json retrieved.
Array ( [data] => Array ( [0] => Array ( [id] => 400327854358866609 [link] => https://www.pinterest.com/r/pin/400327854358866609/4837342446222323658/8737cad9ec4b46439289fbd4f22353f02255549d33a8999691d82b8642f42625 [url] => https://www.pinterest.com/pin/400327854358866609/ [creator] => [board] => [created_at] => [note] => Hello this is simple test [color] => [counts] => [media] => [attribution] => [image] => [metadata] => [original_link] => ) [1] => Array ( [id] => 400327854358814014 [link] => https://www.pinterest.com/r/pin/400327854358814014/4837342446222323658/3f1103b8d1c97de21f913293ee6062f4122313c931b054764167e96745f64465 [url] => https://www.pinterest.com/pin/400327854358814014/ [creator] => [board] => [created_at] => [note] => 55 Awesome Men’s Tattoos | InkDoneRight We’ve collected 55 Awesome Different Mens Tattoo Designs to inspire you! We also have the meaning and symbolism behind the common men’s tattoo designs... [color] => [counts] => [media] => [attribution] => [image] => [metadata] => [original_link] => ) ) [page] => )
In above json image or media key has no values.
If you use the "API explorer", and add the "image" field, one of the outputs is the URL of the original image.
https://api.pinterest.com/v1/boards/BOARD_OWNER/BOARD_NAME/pins/?access_token=MY_TOKEN&fields=id%2Clink%2Cnote%2Curl%2Cattribution%2Cboard%2Ccolor%2Ccreated_at%2Ccreator%2Cimage%2Cmedia%2Cmetadata%2Coriginal_link%2Ccounts
Or using the JS SDK eg:
PDK.request(thisRequest, {fields: 'id,note,link,url,image'}, thisCmd,
function( PDKdata){};
See my example here:
Example of using Pinterest API
I have the below array containing arrays and arrays. This is just one entry; imagine now that I have 20 sources and each source contains at least 10 entries like the one below. This is a nightmare I want to solve.
Can anyone advice on how I can parse this efficiently in PHP and be able to get each of the values to be able to insert them into a DB for further processing!! Thank you.
Array (
[id] => id
[direction] => ltr
[updated] => 1407220315278
[title] => Syria | The Guardian
[alternate] => Array ( [0] => Array (
[href] => href
[type] => text/html ) )
[continuation] => 147a4ddf48e:28d98:bda086f
[items] => Array ( [0] => Array (
[keywords] => Array (
[0] => Global development
[1] => Refugees
[2] => Syria
[3] => Middle East and North Africa
[4] => World news
[5] => UK news
[6] => Scotland
[7] => Conflict and development
[8] => Immigration and asylum )
[fingerprint] => 47075b4f
[originId] => originId
[id] => Te6w3H2VbpKKFda+uYdqytnutphL/kktv0RL7gq9jjU=_147a4ddf48e:28d98:bda086f
[title] => A Syrian refugee in Scotland: 'I'm one of the lucky ones' video
[published] => 1407218400000
[crawled] => 1407220315278
[alternate] => Array (
[0] => Array (
[href] => href
[type] => text/html ) )
[author] => Produced by Claudine Spera and Ellie Violet Bramley
[origin] => Array (
[streamId] => streamId
[title] => Syria | The Guardian
[htmlUrl] => htmlUrl )
[summary] => Array (
[direction] => ltr
[content] => Ayman is one of about 3 million Syrian refugees living outside his homeland. After nine of his friends were killed in Damascus, Ayman used his student visa to flee to the UK, leaving his wife and twin boys behind. 'We didn't expect civil war in Syria, never, ever,' he says. But now, with the help of the Red Cross, the family are reunited and building a life in Edinburgh Continue reading... )
[visual] => Array (
[url] => imagUrl
[height] => 252
[width] => 600
[contentType] => image/jpeg )
[unread] => 1
[categories] => Array (
[0] => Array (
[id] => user/5312f2fe-1adc-476f-93da-8b1db5a63180/category/عربي
[label] => عربي ) )
[engagement] => 4 [engagementRate] => 0.33 ) ) )
If there are always the same keys in the array it should be relatively easy to iterate with something like this:
foreach($yourarray as $key1=>$item1){
//dosomething
if($key1=="alternate"){
foreach($item1 as $key2=>$item2){
//and so on
}
}
}
As to saving it in a database I admit its not easy and I am not that experienced with databases but if I were you I'd save them in multiple tables with "1 to n" references.
I am working with the Yahoo BOSS API to build an image search for motherpipe.co.uk.
I have managed to create a valid request for image listings and have received the response. My problem is that I don't understand how I can use the different elements of that response to build my nice-looking page of image listings.
Ideally I want to loop through the array and display a thumbnail and link for each item in the list but somehow I can't abstract the relevant bits from the $results.
The (example) output from the query with two listings is in this $results:
stdClass Object ( [bossresponse] => stdClass Object ( [responsecode] => 200 [images] => stdClass Object ( [start] => 0 [count] => 2 [totalresults] => 107000 [results] => Array ( [0] => stdClass Object ( [clickurl] => htt://library.thinkquest.org/07aug/01105/Sweden/stockholm.jpg [size] => 191.8KB [format] => jpeg [height] => 586 [refererclickurl] => htt://library.thinkquest.org/07aug/01105/Sweden/sweden_home.html [refererurl] => htt://library.thinkquest.org/07aug/01105/Sweden/sweden_home.html [title] => Stockholm is a beautiful city with Lake Mälaren on it’s WestSide ... [url] => http://library.thinkquest.org/07aug/01105/Sweden/stockholm.jpg [width] => 793 [thumbnailheight] => 118 [thumbnailurl] => htt://ts4.mm.bing.net/th?id=H.4970051277687231&pid=15.1&H=118&W=160 [thumbnailwidth] => 160 ) [1] => stdClass Object ( [clickurl] => http://summerventures.files.wordpress.com/2012/01/429c_stockholm_ch.jpg [size] => 2.3MB [format] => jpeg [height] => 1272 [refererclickurl] => htt://summerventures.wordpress.com/tag/stockholm/ [refererurl] => htt://summerventures.wordpress.com/tag/stockholm/ [title] => stockholm | Summer adventures [url] => htt://summerventures.files.wordpress.com/2012/01/429c_stockholm_ch.jpg [width] => 1800 [thumbnailheight] => 113 [thumbnailurl] => htt://ts2.mm.bing.net/th?id=H.4581116279128437&pid=15.1&H=113&W=160 [thumbnailwidth] => 160 ) ) ) ) )
Question:
What approach can I use to simply display a thumbnail picture with a link from each of the listings in this array, using the info in [thumbnailurl] and [refererclickurl] kind of like
<div> <<a href="[refererclickurl]"><img src="[thumbnailurl]" alt="alt text" /> </div>
I think I need a for each approach but after having tried for three days I can't make it work.
Any help much appreciated.
Try the following. The quoted object in your question is in the $o variable.
$html = '';
$images = $o->bossresponse->images->results;
foreach ($images as $image) {
$html .= "<a href='{$image->refererclickurl}'><img src='{$image->thumbnailurl}'></a>";
}
echo $html;
Note that if $obj->prop equals 'foo' then "{$obj->prop} is not bar" would equal 'foo is not bar'.
Its also good to note that the html would be better if you also output the image dimensions and may change depending on what html spec you're implementing.
Right now, the XML response that I get from the API contains just the size of the clothing corresponding to its ASIN. For the case of ASIN B0069NZPHA, the Size is Small. However if you were to visit its detail page url, you can see that there are 2 sizes, Small and Medium.
Is it possible get all the sizes related to a particular ASIN?
XML Response
SimpleXMLElement Object
(
[ASIN] => B0069NZPHA
[ParentASIN] => B0069O8WN8
[DetailPageURL] => http://www.amazon.com/2b-Haley-Beaded-Top-TANGERINE/dp/B0069NZPHA%3FSubscriptionId%3DAKIAJG3VKWBENQUGEYZA%26tag%3Dyoozu-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0069NZPHA
...
[ItemAttributes] => SimpleXMLElement Object
(
[Binding] => Apparel
[Brand] => 2b by bebe
[CatalogNumberList] => SimpleXMLElement Object
(
[CatalogNumberListElement] => 192612
)
[Color] => Tangerine
[Department] => womens
[Feature] => Array
(
[0] => SKU 192612
[1] => Style 60HZK201N240
)
[Label] => bebe
[Manufacturer] => bebe
[PackageDimensions] => SimpleXMLElement Object
(
[Height] => 230
[Length] => 1120
[Weight] => 35
[Width] => 670
)
[ProductGroup] => Apparel
[ProductTypeName] => SHIRT
[Publisher] => bebe
[Size] => Small
[Studio] => bebe
[Title] => 2b Haley Beaded Top - TANGERINE (S)
)
I would check ItemLookup and especially the ResponseGroup = Variations / VariationsSummary. (Or try any other ResponseGroup, maybe some group will show all the sizes)
I used Variations response group (ItemLookup) with ParentASIN to get all of sizes and colors