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.
Related
I'm setting up a Cloudinary listing of uploaded videos and I want to get the metadata of that video.
I have a print_r($result):
<pre>
Cloudinary\Api\Response Object
(
[rate_limit_reset_at] => 123123123
[rate_limit_allowed] => 123
[rate_limit_remaining] => 123
[storage:ArrayObject:private] => Array
(
[public_id] => dog
[format] => mp4
[version] => 123123123
[resource_type] => video
[type] => upload
[created_at] => 2019-07-29T07:32:50Z
[bytes] => 123123
[width] => 321
[height] => 456
[backup] => 1
[access_mode] => public
[url] => http://res.cloudinary.com/demo/video/upload/dog.mp4
[secure_url] => https://res.cloudinary.com/demo/video/upload/dog.mp4
[next_cursor] => 123123123
[derived] => Array
(
[0] => Array
(
[transformation] => /jpg
[format] => jpg
[bytes] => 86438
[id] => 123123123
[url] => http://res.cloudinary.com//demo/video/upload/dog.jpg
[secure_url] => https://res.cloudinary.com/demo/video/upload/dog.mp4
[extension] => jpg
)
[1] => Array
(
[transformation] => t_media_lib_thumb/jpg
[format] => jpg
[bytes] => 3293
[id] => 12123123
[url] => https://res.cloudinary.com/demo/video/upload/dog.jpg
[secure_url] => https://res.cloudinary.com/demo/video/upload/dog.jpg
[extension] => jpg
)
)
)
)
</pre>
I tried getting the value using this format but it says undefined index. echo $result[4]['width'] or echo $result['storage:ArrayObject:private']['width'];
I talked to a cloudinary support ad he said that "you should be able to access the data returned within [storage:ArrayObject:private] by simply ignoring it and requesting the index that you desire directly from $result. Meaning you can use or and so on with the other paramters."
So basically, just removing the index solved my problem like so:
<?php echo $result['height']; ?> <?php echo $result['width']; ?>
Have a look at this https://github.com/cloudinary/cloudinary_php/blob/master/src/Api/Response.php in order to better understand the object you are getting back.
Afterwards, look at the ArrayObject documentation here https://www.php.net/manual/en/class.arrayobject.php, as your Response object extends this class. Using getArrayCopy() to transform your object into an array should be what you need.
Bear in mind that I am not familiar with the API and that there may be better options to get what you need.
am unable to use the path returned by drupal 7 for an image which was uploaded for custom content type. the path returned is as follow:
Array ( [und] => Array ( [0] => Array ( [fid] => 6 [alt] => [title] => [width] => 464 [height] => 261 [uid] => 1 [filename] => _71135631_c0166859-snotty_child_with_a_cold-spl.jpg [uri] => public://_71135631_c0166859-snotty_child_with_a_cold-spl.jpg [filemime] => image/jpeg [filesize] => 22168 [status] => 1 [timestamp] => 1384797114 [rdf_mapping] => Array ( ) ) ) )
but when using the vale of uri, the image is not shown.
any assistance appreciated.
regards,
a.ali
Such uri-fields in Drupal always refer to the internal data-storage logic, hence the public:// at the beginning. Drupal uses Image Styles for any images attached to content.
To retrieve the full URL for an image, you should use image_style_url():
$imgPath = image_style_url('default', $object['und'][0]['uri']);
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
The best way for me to explain this is to show you. Seems like a float() error in a 64bit system.
when i call /anotherfeed/feed or any page for that matter, posts with story_tags return some of the id's as a float error.
sample story tag with float error in id. [id] => 1.7153566624E+14
My question is, how do i fix this, or what am i doing wrong? all i am doing is looping in a foreach statement.
if($fvalue[story_tags]){
echo 'Tags: ';
$sTags=$fvalue[story_tags];
foreach ($sTags as $skey=>$svalue){
foreach ($svalue as $gkey=>$hvalue){
$id=$hvalue[id];
echo ''.$hvalue[name].' '.$id.' ';
}
}
}
[story_tags] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 1.7153566624E+14
[name] => Another Feed
[offset] => 0
[length] => 12
[type] => page
)
)
Array
(
[data] => Array
(
[0] => Array
(
[id] => 171535666239724_156133294510726
[from] => Array
(
[name] => Another Feed
[category] => App page
[id] => 171535666239724
)
[story] => Another Feed shared Non-Profits on Facebook's photo.
[story_tags] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 1.7153566624E+14
[name] => Another Feed
[offset] => 0
[length] => 12
[type] => page
)
)
[20] => Array
(
[0] => Array
(
[id] => 41130665917
[name] => Non-Profits on Facebook
[offset] => 20
[length] => 23
[type] => page
)
)
)
[picture] => http://photos-d.ak.fbcdn.net/hphotos-ak-ash3/557037_10150932300320918_1908237167_s.jpg
[link] => http://www.facebook.com/photo.php?fbid=10150932300320918&set=a.85612830917.95996.41130665917&type=1
[name] => Wall Photos
[caption] => Have you heard of the #[159208207468539:274:One Day without Shoes] (ODWS) campaign? ODWS is an annual initiative by #[8416861761:274:TOMS] to bring awareness around the impact a pair of shoes can have on a child's life.
During the 2012 campaign, #[20531316728:274:Facebook] drove 20% of traffic to the ODWS microsite and TOMS even launched a Facebook-exclusive "Barefoot & Blue" giveaway with #[25266987484:274:Essie Nail Polish] for the second year in a row.
We think this is a pretty cool example of creating exclusive content around an important initiative that keeps people engaged and involved!
[properties] => Array
(
[0] => Array
(
[name] => By
[text] => Non-Profits on Facebook
[href] => http://www.facebook.com/nonprofits
)
)
[icon] => http://static.ak.fbcdn.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif
[type] => photo
[object_id] => 10150932300320918
[application] => Array
(
[name] => Photos
[id] => 2305272732
)
[created_time] => 2012-07-02T17:57:23+0000
[updated_time] => 2012-07-02T17:57:23+0000
[comments] => Array
(
[count] => 0
)
)
solution:
cURL - had to use number format with PHP_EOL to solve in cURL.
// $locs = curl call to graph api for /anotherfeed/feed, still need solution for foreach.
$locs=json_decode($returned, true);
$stId=number_format($locs[data][1][story_tags][0][0][id], 0, '', '').PHP_EOL;
echo $stId;
PHP-SDK
solution is same, long numbers in the foreach loop need to be ran through number_format.
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";
}