Can someone help me fix this code? result only appear 1 picture..
function get_instagram($q,$client_id) {
$api = "https://api.instagram.com/v1/tags/".$q."/media/recent?client_id=".$client_id;
$response = get_curl($api);
$images = array();
if($response){
foreach(json_decode($response)->data as $item){
$src = $item->images->standard_resolution->url;
$thumb = $item->images->thumbnail->url;
$url = $item->link;
$images[] = array(
"src" => htmlspecialchars($src),
"thumb" => htmlspecialchars($thumb),
"url" => htmlspecialchars($url)
);
return "<a href='".$url."' target='_blank'><img src='".$thumb."' border='0'/></a>";
}
}
}
What I'm trying to achieve is make all result appear in a page
In your code, you are getting only one image because, you are returning image inside loop so it dont executes all the records inside loop. Try below code
function get_instagram($q,$client_id) {
$api = "https://api.instagram.com/v1/tags/".$q."/media/recent?client_id=".$client_id;
$response = get_curl($api);
$images = array();
$returnval = '';
if($response){
foreach(json_decode($response)->data as $item){
$src = $item->images->standard_resolution->url;
$thumb = $item->images->thumbnail->url;
$url = $item->link;
$images[] = array(
"src" => htmlspecialchars($src),
"thumb" => htmlspecialchars($thumb),
"url" => htmlspecialchars($url)
);
$returnval .= "<a href='".$url."' target='_blank'><img src='".$thumb."' border='0'/></a>";
}
}
return $returnval;
}
The way you are combining the images array is not concatenating the images. To do so, use the array_merge() function. Below is the result:
function get_instagram($q,$client_id) {
$api = "https://api.instagram.com/v1/tags/".$q."/media/recent?client_id=".$client_id;
$response = get_curl($api);
$images = array();
if($response){
foreach(json_decode($response)->data as $item){
$src = $item->images->standard_resolution->url;
$thumb = $item->images->thumbnail->url;
$url = $item->link;
array_merge($images, array(
"src" => htmlspecialchars($src),
"thumb" => htmlspecialchars($thumb),
"url" => htmlspecialchars($url)
));
}
foreach ($images as $image) {
echo "<a href='".$image->$url."' target='_blank'><img src='".$image->$thumb."' border='0'/></a>";
}
}
In addition, the return statement would only execute once. In order to print "all" images, use a foreach loop for the $images array.
Related
I am making parser of articles and I need to put all parsed data in josn. I tried to put them to array and then transform it in JSON, but I have some troubles. I get JSON like this:
[{"title":"title1"}][{"title":"title2"}][{"title":"title3"}]
But I want like this:
[{"title":"title1"},{"title":"title2"},{"title":"title3"}]
How I can do this?
<?
foreach ($content_prev as $el) {
$pq = pq($el);
$date = $pq->find('time')->html();
$title = $pq->find('h3 a')->html();
$link = $pq->find('h3 a')->attr('href');
$data_link = file_get_contents($link);
$document_с = phpQuery::newDocument($data_link);
$content = $document_с->find('.td-post-content');
$arr = array (
array(
"title" => $title
),
);
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
}
Try to remove one array in $arr
Use below one.
<?
foreach ($content_prev as $el) {
$pq = pq($el);
$date = $pq->find('time')->html();
$title = $pq->find('h3 a')->html();
$link = $pq->find('h3 a')->attr('href');
$data_link = file_get_contents($link);
$document_с = phpQuery::newDocument($data_link);
$content = $document_с->find('.td-post-content');
$arr[] = array (
"title" => $title
);
}
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
I can list all the images from a folder in json using the code below:
<?php
$dir = "images/friday/";
$images = glob($dir."*.jpg");
foreach($images as $image){
$registro = array(
"FILE" => $image
);
$retorno[] = $registro;
}
$retorno = json_encode($retorno);
echo $retorno;
?>
I think it would work with directories too but nothing is displayed if I don´t use print_r($dirs).
<?php
$path = "images";
$dirs = glob($path . "/*", GLOB_ONLYDIR);
foreach($dirs as $dir){
$reg = array(
"FOLDER" => $dir
);
$return[] = $reg;
}
$return = json_encode($return);
echo $return;
?>
echo $return does not display anything. Is there a simple workaround or I have to build something like this function? I need the "FOLDER" identifier.
I know this solution is simple, but it keeps slipping my mind. When I parse the page with this code and the $links array is printed, all of href parts are correct yet the img part only prints the last src element that is found on the page.
$doc = new DOMDocument();
$doc->loadHTML($html);
$links = array();
$images = $doc->getElementsByTagName("img");
$arr = $doc->getElementsByTagName("a");
foreach($arr as $item) {
// get links
$href = $item->getAttribute("href");
// get images.
foreach ($images as $item) {
$img = $item->getAttribute('src');
}
$links[] = array(
'href' => $href,
'img' => $img
);
}
print_r(array_values($links));
The for each statement for images should be building an array where as the final array ($links)is a multi-dimentional array($img being the nested array).
Check if this works for you:
$doc = new DOMDocument();
$doc->loadHTML($html);
$links = array();
$images = $doc->getElementsByTagName("img");
$arr = $doc->getElementsByTagName("a");
foreach($arr as $item) {
// get links
$href = $item->getAttribute("href");
// get images.
foreach ($images as $item) {
$img = $item->getAttribute('src');
// storing the image src
$links[] = array(
'img' => $img
);
}
$links[] = array(
'href' => $href
);
}
print_r(array_values($links));
You use the dublicate variable $item in internal foreach.
Try this without internal foreach
$doc = new DOMDocument();
$doc->loadHTML($html);
$links = array();
$images = $doc->getElementsByTagName("img");
$arr = $doc->getElementsByTagName("a");
foreach($arr as $key=>$item) {
// get links
$href = $item->getAttribute("href");
$img = $images[$key]->getAttribute('src');
$links[] = array(
'href' => $href,
'img' => $img
);
}unset($item);
print_r(array_values($links));
I'm trying to get images from flickr using the Flickr API and I'm having trouble figuring out how to get only unique images. I've already reviewed the arguments for the specific method that I'm using and here's what I came up with:
<?php
class Flickr {
private $flickr_key;
private $flickr_secret;
private $format = 'json';
public function __construct( $flickr_key, $flickr_secret ) {
$this->flickr_key = $flickr_key;
$this->flickr_secret = $flickr_secret;
}
public function searchPhotos( $query = '', $tags = '' ) {
$urlencoded_tags = array();
$tags_r = explode(',', $tags);
foreach($tags_r as $tag){
$urlencoded_tags[] = urlencode($tag);
}
$url = 'http://api.flickr.com/services/rest/?';
$url .= 'method=flickr.photos.search';
$url .= '&text=' . urlencode($query);
$url .= '&tags=' . implode(',', $urlencoded_tags);
$url .= '&sort=relevance';
$url .= '&safe_search=1';
$url .= '&content_type=4';
$url .= '&api_key=' . $this->flickr_key;
$url .= '&format=' . $this->format;
$url .= '&per_page=10';
$url .= '&media=photos';
$url .= '&privacy_filter=1';
$result = #file_get_contents( $url );
$json = substr( $result, strlen( "jsonFlickrApi(" ), strlen( $result ) - strlen( "jsonFlickrApi(" ) - 1 );
$photos = array();
$data = json_decode( $json, true );
if($data['stat'] != 'fail'){
$photos = $data['photos']['photo'];
return $photos;
}else{
return false;
}
}
}
And I'll just call it in like:
$flickr = new Flickr($flickr_key, $flickr_secret);
$query = 'Kaspersky Internet Security 2013';
$tags = 'software';
$results = $flickr->searchPhotos($query, $tags);
foreach($results as $img){
$src = "http://farm" . $img['farm'] . ".static.flickr.com/" . $img['server'] . '/' . $img['id'] . '_' . $img['secret'] . '_m.jpg';
?>
<img src="<?php echo $src; ?>"/>
<?php
}
The problem here is that I'm getting duplicate images from time to time.
I also tried using the phpflickr library. But I'm still having the same issues:
$flickr = new phpFlickr($api_key);
$args = array(
'text' => 'Kaspersky Internet Security 2013',
'tags' => 'software',
'per_page' => '10',
'safe_search' => '1',
'content_type' => '4',
'media' => 'photos',
'sort' => 'relevance',
'privacy_filter' => '1'
);
$results = $flickr->photos_search($args);
$hashes = array();
$sources = array();
$images = $results['photo'];
foreach($images as $img){
$src = "http://farm" . $img['farm'] . ".static.flickr.com/" . $img['server'] . '/' . $img['id'] . '_' . $img['secret'] . '_m.jpg';
$current_hash = sha1_file($src);
if(!in_array($current_hash, $hashes)){
?>
<img src="<?php echo $src; ?>" alt="">
<?php
}
$hashes[] = $current_hash;
}
As you can see from the above code I've used sha1_file method to compare the hashes of each of the images returned from flickr. But that's a big performance hit:
without sha1_file: 0.81311082839966
with sha1_file: 6.8974900245667
Any ideas what else can I do to prevent flickr from returning duplicates? As you can see I'm only returning 10 images and that's all I need. I've also tried to add as many arguments that matches my needs but still no luck. Thanks in advance!
try on API explorer, will this give you the same result?
I tried using the param you specified, it returns 34 result in total..
Im using the PHP below to generate some HTML output:
<?php
$url = "images.xml";
$xmlstr = file_get_contents($url);
$xml = new SimpleXMLElement($xmlstr);
$images = array();
$ids = array();
foreach ($xml->image as $image) {
$images[]['id'] = $image -> id;
$images[]['link'] = $image->href;
$images[]['src'] = $image->source;
$images[]['title'] = $image->title;
$images[]['alt'] = $image->alt;
$ids[] = $image -> id;
}
array_multisort($ids, SORT_ASC, $images);
foreach ($images as $image){
echo "<a href='".$image['link']."'><img src='".$image['src']."' alt='".$image['alt']."' title='".$image['title']."' /></a>";
}
?>
If I change the code here:
foreach ($images as $image){
echo $image['link'];
echo "Item";
}
I get the image link 3 times, which is correct because there are 3 records in the XML. But I get 12 copies of the text Item.
Why is this happening?
You're putting each attribute in a new row in the array.
Try this:
foreach ($xml->image as $image)
{
$images[] = array(
'id' => $image->id,
'link' => $image->href,
'src' => $image->source,
'title' => $image->title,
'alt' => $image->alt
);
$ids[] = $image -> id;
}