I have come to some problems. I've made a wordpress plugin, which automatically gets the most 20 recent Instagram posts and then, in theory, it should make me let to insert the newest image as a shortcode in to the post. Now, the code to reproduce this is:
//define Access token
$accesst= "PUT YOUR INSTAGRAM ACCESS TOKEN HERE";
//userid
$userid= YOUR INSTAGRAM USER ID HERE;
//image count to get
$count=20;
//get api contents
$content = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$accesst.'&count='.$count);
//converting JSON to object
$standardres = json_decode($content, true);
//array method
foreach($standardres['data'] as $photo) {
$imageData = base64_encode(file_get_contents($photo['images']['standard_resolution']['url']));
$images[] = '<img src="data:image/jpeg;base64,' . $imageData . '" />';
}
//create functions for shortcodes
function fone($images){
return $images[0]; //naudok tik [one]
}
//shortcodes
add_shortcode( 'one', 'fone');
?>
Basically, I get an error message displaying:
Notice: Uninitialized string offset: 0 in D:\XEMP\htdocs\xd\wordpress\wp-content\plugins\insta-live\insta-live.php on line 29
Any ideas how to solve this? A var_dump() gives me the images above the header.. And please don't point me to unitiliazed string offset thread, because i don't really see it as the same problem.
I haven't used WordPress for awhile but $images looks out of scope. I would maybe try wrapping your API work and referencing it inside the shortcode function, something like below. I would research best practice on this type of thing related to WordPress:
if(!class_exists('MyAPI')) {
class MyAPI
{
# Create an image storage
protected static $imgs;
# Access your API
public function callInstagram($accesst = 'PUT YOUR INSTAGRAM ACCESS TOKEN HERE',$userid = 'YOUR INSTAGRAM USER ID HERE')
{
# If you have already set it with content, return it
if(!empty(self::$imgs['instagram']))
return self::$imgs['instagram'];
//image count to get
$count = 20;
//get api contents
$content = file_get_contents('https://api.instagram.com/v1/users/self/media/recent/?access_token='.$accesst.'&count='.$count);
//converting JSON to object
$standardres = json_decode($content, true);
//array method
foreach($standardres['data'] as $photo) {
$imageData = base64_encode(file_get_contents($photo['images']['standard_resolution']['url']));
$images[] = '<img src="data:image/jpeg;base64,' . $imageData . '" />';
}
# Set the instagram images store
if(!empty($images))
# Assign
self::$imgs['instagram'] = $images;
# Return the images if set
return (isset(self::$imgs['instagram']))? self::$imgs['instagram'] : false;
}
# Return the images
public function getInstagramImg($img = false)
{
$imgs = $this->callInstagram();
if($img !== false)
return (isset($imgs[$img]))? $imgs[$img] : false;
# Return all
return $imgs;
}
}
}
//create functions for shortcodes
function fone()
{
# Create API instance
$Instagram = new MyAPI();
# Send back the first image in the list
return $Instagram->getInstagramImg('0');
}
//shortcodes
add_shortcode('one', 'fone');
One last note, I am assuming that your API work is correct, you should check that it works first before you start going all crazy trying to figure out why the $images doesn't work. Use print_r() to see if it returns the correct info from Instagram.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With