PHP 错误崩溃代码

PHP error crashing code

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

//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() 给了我 header 上方的图像。请不要将我指向统一的字符串偏移线程,因为我真的不认为它是同一个问题。

看来您对进入 fone() 函数的输入有问题,这会触发该错误。

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

示例...

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

我有一段时间没有使用 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() 查看它是否 returns 来自 Instagram 的正确信息。

在循环和附加值之前初始化 $images,特别是因为您想将其作为参数传递。在 foreach 循环之前,添加:

$images = array();

感谢大家的帮助,看看你是否想在工作状态下查看整个代码:

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

?>

如果您想使用它,您可以随意使用,只需将 "PUT_YOUR_ACCESS_TOKEN_HERE" 和 "PUT_YOUR_USER_ID_HERE" 替换为您的 Instagram 访问令牌和您的用户 ID!