How can I go about convering a public instagram URL into JSON using PHP? Ex: https://www.instagram.com/explore/tags/brindle/
I can't use the API as I need public hashtag content and my use case won't qualify for their app review process :-(.
Here is what I have so far but it does not pull all images. Also, I'd like to be able to load the "load more" images as well. Any help would be much appreciated!
$instagram_source = file_get_contents("https://www.instagram.com/explore/tags/brindle/");
$instagram_data = explode("window._sharedData = ", $instagram_source);
$instagram_json = explode(';</script>', $instagram_data[1]);
$instagram_array = json_decode($instagram_json[0], TRUE);
$instagram_media = $instagram_array['entry_data']['TagPage'][0]['tag']['media']['nodes'];
if(!empty($instagram_media)) {
echo '<ul>';
foreach($instagram_media as $im) {
echo '<li>';
echo '<a href="https://www.instagram.com/p/'.$im['code'].'/" target="_blank">';
echo '<img src="'.$im["display_src"].'" alt="" width="'.$im["dimensions"]["width"].'" height="'.$im["dimensions"]["height"].'" />';
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
Take a look at this solution here: https://github.com/Bolandish/Instagram-Grabber
Thats the best one i know until now.
Related
I'm doing a PHP assignment having a very basic knowledge of PHP.
I'm looping through a list of pictures from S3, but after looping through I want a selected picture to be opened on click. I'm clueless on how it will proceed further.
I have a product.php page which has to redirect to single .php.
PHP is server side language that send html tags to render in browser side, so you can use client side languages (exp: javascript) to handle it.
You can do it with HTML: Anchors aka Links:
$images = [
'a.jpg',
'b.jpg'
];
echo '<ul>';
foreach($images as $image) {
echo '<li>';
echo '<a href="single.php?image=' . $image . '">';
// small preview-images could be prefixed with 'thumb_' . $image
echo '<img src="' . $image . '" width='100'/>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
In single.php you will get the clicked Image with $_GET['image']
I'm working on a project and it's something new for me. I'll need to fetch rss content from websites, and display Descripion, Title and Images (Thumbnails). Right now i've noticed that some feeds show thumbnails as Enclosure tag and some others dont. right now i have the code for both, but i need to understand how i can create a conditional like:
If the rss returns enclosure image { Do something }
Else { get the common thumb }
Here follow the code that grab the images:
ENCLOSURE TAG IMAGE:
if ($enclosure = $block->get_enclosure())
{
echo "<img src=\"" . $enclosure->get_link() . "\">";
}
NOT ENCLOSURE:
if ($enclosure = $block->get_enclosure())
{
echo '<img src="'.$enclosure->get_thumbnail().'" title="'.$block->get_title().'" width="200" height="200">';
}
=================================================================================================
PS: If we look at both codes they're almost the same, the difference are get_thumbnail and get_link.
Is there a way i can create a conditional to use the correct code and always shows the thumbnail?
Thanks everyone in advance!
EDITED
Here is the full code i have right now:
include_once(ABSPATH . WPINC . '/feed.php');
if(function_exists('fetch_feed')) {
$feed = fetch_feed('http://feeds.bbci.co.uk/news/world/africa/rss.xml'); // this is the external website's RSS feed URL
if (!is_wp_error($feed)) : $feed->init();
$feed->set_output_encoding('UTF-8'); // this is the encoding parameter, and can be left unchanged in almost every case
$feed->handle_content_type(); // this double-checks the encoding type
$feed->set_cache_duration(21600); // 21,600 seconds is six hours
$feed->handle_content_type();
$limit = $feed->get_item_quantity(18); // fetches the 18 most recent RSS feed stories
$items = $feed->get_items(0, $limit); // this sets the limit and array for parsing the feed
endif;
}
$blocks = array_slice($items, 0, 3); // Items zero through six will be displayed here
foreach ($blocks as $block) {
//echo $block->get_date("m d Y");
echo '<div class="single">';
if ($enclosure = $block->get_enclosure())
{
echo '<img class="image_post" src="'.$enclosure->get_link().'" title="'.$block->get_title().'" width="150" height="100">';
}
echo '<div class="description">';
echo '<h3>'. $block->get_title() .'</h3>';
echo '<p>'.$block->get_description().'</p>';
echo '</div>';
echo '<div class="clear"></div>';
echo '</div>';
}
And here are the XML pieces with 2 different tags for images:
Using Thumbnails: view-source:http://feeds.bbci.co.uk/news/world/africa/rss.xml
Using Enclosure: http://feeds.news24.com/articles/news24/SouthAfrica/rss
Is there a way i can create a conditional to use the correct code and always shows the thumbnail?
Sure there is. You've not said in your question what blocks you so I have to assume the reason, but I can imagine multiple.
Is the reason a decisions with more than two alternations?
You handle the scenario of a feed item having no image or an image already:
if ($enclosure = $block->get_enclosure())
{
echo '<img class="image_post" src="'.$enclosure->get_link().'" title="'.$block->get_title().'" width="150" height="100">';
}
With your current scenario there is only one additional alternation which makes it three: if the enclosure is a thumbnail and not a link:
No image (no enclosure)
Image from link (enclosure with link)
Image from thumbnail (enclosure with thumbnail)
And you then don't know how to create a decision of that. This is what basically else-if is for:
if (!$enclosure = $block->get_enclosure())
{
echo "no enclosure: ", "-/-", "\n";
} elseif ($enclosure->get_link()) {
echo "enclosure link: ", $enclosure->get_link(), "\n";
} elseif ($enclosure->get_thumbnail()) {
echo "enclosure thumbnail: ", $enclosure->get_thumbnail(), "\n";
}
This is basically then doing the output based on that. However if you assign the image URL to a variable, you can decide on the output later on:
$image = NULL;
if (!$enclosure = $block->get_enclosure())
{
// nothing to do
} elseif ($enclosure->get_link()) {
$image = $enclosure->get_link();
} elseif ($enclosure->get_thumbnail()) {
$image = $enclosure->get_thumbnail();
}
if (isset($image)) {
// display image
}
And if you then move this more or less complex decision into a function of it's own, it will become even better to read:
$image = feed_item_get_image($block);
if (isset($image)) {
// display image
}
This works quite well until the decision becomes even more complex, but this would go out of scope for an answer on Stackoverflow.
I'm aware that there is an amazon API for pulling their data but I'm just trying to learn to scrape for my own knowledge and pulling data from amazon seems like a good test.
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
include('../includes/simple_html_dom.php');
$html = file_get_html('http://www.amazon.co.uk/gp/product/B00AZYBFGY/ref=s9_simh_gw_p86_d0_i1?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-2&pf_rd_r=1MP0FXRF8V70NWAN3ZWW&pf_r$')
foreach($html->find('a-section') as $element) {
echo $element->plaintext . '<br />';
}
echo $ret;
?>
All I'm trying to do is pull the product description from the link but I'm not sure why it's working. I'm not getting any errors or any data at all, really.
The class for the Product Description is simply productDescriptionWrapper so in your sample code use that css selector
foreach($html->find('.productDescriptionWrapper') as $element) {
echo $element->plaintext . '<br />';
}
simplehtmldom uses css selectors very similar to jQuery. so if you want all divs say ->find('div') if you want all anchors with a class of 'hotProduct' say ->find('a.hotProduct') so on and so forth
It doesn't work because the product description is being added by JavaScript into an iFrame.
You first can check if there is an HTML taken from the Amazon. It might block your request.
$url = "https://www.amazon.co.uk/gp/product/B00AZYBFGY/ref=s9_simh_gw_p86_d0_i1?pf_rd_m=A3P5ROKL5A1OLE&pf_rd_s=center-2&pf_rd_r=1MP0FXRF8V70NWAN3ZWW&pf_r$"
$htmlContent = file_get_contents($url);
echo $htmlContent;
$html = str_get_html($htmlContent);
Note, the https://, you have http://, maybe that is why you get nothing.
Once you get HTML, you can go forward.
Try different selectors:
foreach($html->find('div[id=productDescription]')) as $element) {
echo $element->plaintext . '<br />';
}
foreach($html->find('div[id=content]')) as $element) {
echo $element->plaintext . '<br />';
}
foreach($html->find('div[id=feature-bullets]')) as $element) {
echo $element->plaintext . '<br />';
}
It should display the page itself, maybe with some missing CSS.
If the HTML is in place. You can try those xpaths
How can I get the title and all the meta tags from a Facebook page?
I have this code:
function getMetaData($url){
// get meta tags
$meta=get_meta_tags($url);
// store page
$page=file_get_contents($url);
// find where the title CONTENT begins
$titleStart=strpos($page,'<title>')+7;
// find how long the title is
$titleLength=strpos($page,'</title>')-$titleStart;
// extract title from $page
$meta['title']=substr($page,$titleStart,$titleLength);
// return array of data
return $meta;
}
$tags=getMetaData('https://www.facebook.com/showmeapp?filter=3');
echo 'Title: '.$tags['title'];
echo '<br />';
echo 'Description: '.$tags['description'];
echo '<br />';
echo 'Keywords: '.$tags['keywords']
Example: https://www.facebook.com/showmeapp?filter=3
You really donĀ“t need to deal with the Metatags, just call the Graph API:
$data = file_get_contents('https://graph.facebook.com/bladauhu'); //example page
Works even without an App, for every public page.
i think url is not correct change your url to https://graph.facebook.com/showmeapp
Use Simple HTML DOM Parser
http://simplehtmldom.sourceforge.net/
Try below code:
<?php
require_once('simple_html_dom.php');
$page=$html = file_get_html('https://www.facebook.com/showmeapp');
$title= $html->find('meta[name="title"]',0)->content;
echo 'Title: '.$title ;
echo '<br />';
$description= $html->find('meta[name="description"]',0)->content;
echo 'Description: '.$description;
echo '<br />';
$keywords= $html->find('meta[name="keywords"]',0)->content;
echo 'Keywords: '.$keywords;
?>
I am trying to create some text based share buttons for my Wordpress that also output the shared amount. So far I got it working with Facebook and Delicious but I am not sure how to get it going with Twitter.
This is what I did for Delicious.
<?php
$shareUrl = urlencode(get_permalink($post->ID));
$shareTitle = urlencode($post->post_title);
$deliciousStats = json_decode(file_get_contents('http://feeds.delicious.com/v2/json/urlinfo/data?url='.$shareUrl));
?>
<a onclick='window.open("http://delicious.com/save?v=5&noui&jump=close&url=<?php echo $shareUrl; ?>&title=<?php echo $shareTitle; ?>", "facebook", "toolbar=no, width=550, height=550"); return false;' href='http://delicious.com/save?v=5&noui&jump=close&url=<?php echo $shareUrl; ?>&title=<?php echo $shareTitle; ?>' class='delicious'>
<?php
if($deliciousStats[0]->total_posts == 0) {
echo 'Save';
} elseif($deliciousStats[0]->total_posts == 1) {
echo 'One save';
} else {
echo $deliciousStats[0]->total_posts.' saves';
}
?>
</a>
I also got the API Url which calls the tweeted numbers and URL.
http://urls.api.twitter.com/1/urls/count.json?url=SOMESITEURLHERE&callback=twttr.receiveCount
Basically it calls the JSON encoded file, and then gives you the option to share the link in <A></A> tags but instead of showing some text such as Share, it will show the count instead. I'm basically creating some CSS share buttons.
Just use Twitter's own tweet button.
It does that for you and you can style it with .twitter-share-button
(I would post this as a reply but I don't have the privilege.)
Probably you have figured out a solution yourself by now. I just had the same problem and solved it this way:
$handle = fopen('http://urls.api.twitter.com/1/urls/count.json?url=nu.nl', 'rb');
$twitCount = json_decode(stream_get_contents($handle));
fclose($handle);
print_r($twitCount->count);
function get_tweets($url) {
$json_string = file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $url);
$json = json_decode($json_string, true);
return intval( $json['count'] );
}
function total($url){
return get_tweets($url); }
Then, use this to get the twitte share count in required place.
<?php echo total("http://website.com/"); ?>