PHP错误导致代码崩溃

时间:2017-09-05 18:08:56

标签: php wordpress crash instagram-api

我遇到了一些问题。我已经制作了一个wordpress插件,它会自动获取最近的20个Instagram帖子,然后理论上它应该让我将最新的图像作为短代码插入到帖子中。 现在,重现这个的代码是:

//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');
?>

基本上,我收到一条显示错误消息:

Notice: Uninitialized string offset: 0 in D:\XEMP\htdocs\xd\wordpress\wp-content\plugins\insta-live\insta-live.php on line 29

任何想法如何解决这个问题?一个var_dump()给了我标题上方的图片..请不要指向我单元化的字符串偏移线程,因为我并不认为它是同样的问题。

4 个答案:

答案 0 :(得分:1)

我还没有使用WordPress一段时间,但$images看起来超出了范围。我可能会尝试包装你的API工作并在短代码函数中引用它,如下所示。我会研究与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');

最后一点,我假设您的API工作正确,您应该先检查它是否有效,然后才开始疯狂地试图找出$images无效的原因。使用print_r()查看它是否从Instagram返回正确的信息。

答案 1 :(得分:1)

在循环并附加值之前初始化$images,尤其是因为您希望将其作为参数传递。在foreach循环之前,添加:

$images = array();

答案 2 :(得分:0)

如果输入进入导致该错误的fone()函数,则会出现问题。

尝试在该函数中执行isset()检查,以确保在您尝试返回该项之前该项确实存在。

示例...

//create functions for shortcodes
function fone($images){
    if (isset($images[0])) {
        return $images[0]; //naudok tik [one]
    }
    return '';
} 

答案 3 :(得分:0)

感谢帮助人员,检查您是否希望看到整个代码处于工作状态:

<?php
/*
Plugin Name: Instagram Feed Embed
Description: Embed a live photo to wordpress posts from your feed
Version: 0.1.0
Author: Jacob Stankaitis
Author URL: https://www.upwork.com/freelancers/~017e31b991d3d0f253
*/
//define Access token
$accesst= "PUT_YOUR_ACCESS_TOKEN_HERE";
//userid
$userid= PUT_YOUR_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
$images= array();
foreach($standardres['data'] as $photo) {
   $imageData = base64_encode(file_get_contents($photo['images']['standard_resolution']['url']));
   array_push ($images, '<img src="data:image/jpeg;base64,' . $imageData . '" />');
}

//create functions for shortcodes with definition
function fone(){
    global $images;
    return ($images[0]);
} 
function ftwo(){
global $images;
return $images[1];
}
function fthree(){
    global $images;
return $images [2];

}
function ffour(){
    global $images;
return $images [3];
}
function ffive(){
    global $images;
return $images [4];
}
function fsix(){
    global $images;
return $images [5];
}
function fseven(){
    global $images;
return $images [6];
}
function feight(){
    global $images;
return $images [7];
}
function fnine(){
    global $images;
return $images [8];
}
function ften(){
    global $images;
return $images[9];
}
function feleven(){
    global $images;
return $images [10];
}
function ftwelve(){
    global $images;
return $images [11];
}
function fthirteen(){
    global $images;
return $images[12];
}
function ffourteen(){
    global $images;
return $images [13];
}
function ffifteen(){
    global $images;
return $images [14];
}
function fsixteen(){
    global $images;
return $images [15];
}
function fseventeen(){
    global $images;
return $images [16];
}
function feighteen(){
    global $images;
return $images [17];
}
function fnineteen(){
    global $images;
return $images [18];
}
function ftwenty(){
    global $images;
return $images [19];
}

//create shortcode
add_shortcode( 'one', 'fone');
add_shortcode( 'two', 'ftwo');
add_shortcode( 'three', 'fthree');
add_shortcode( 'four', 'ffour');
add_shortcode( 'five', 'ffive');
add_shortcode( 'six', 'fsix');
add_shortcode( 'seven', 'fseven');
add_shortcode( 'eight', 'feight');
add_shortcode( 'nine', 'fnine');
add_shortcode( 'ten', 'ften');
add_shortcode( 'eleven', 'feleven');
add_shortcode( 'twelve', 'ftwelve');
add_shortcode( 'thirteen', 'fthirteen');
add_shortcode( 'fourteen' , 'ffourteen');
add_shortcode( 'fifteen', 'ffifteen');
add_shortcode( 'sixteen', 'fsixteen');
add_shortcode( 'seventeen', 'fseventeen');
add_shortcode( 'eighteen', 'feighteen');
add_shortcode( 'nineteen', 'fnineteen');
add_shortcode( 'twenty', 'ftwenty');

?>

如果你想使用它,你可以这样做,只是你的Instagram的访问令牌,用户ID替换“PUT_YOUR_ACCESS_TOKEN_HERE”和“PUT_YOUR_USER_ID_HERE!”