PHP错误崩溃代码

PHP错误崩溃代码,php,wordpress,crash,instagram-api,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

我遇到了一些问题。我制作了一个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');
?>

有什么办法解决这个问题吗?
var\u dump()
为我提供了标题上方的图像。。请不要将我指向Unitilized string offset thread,因为我并不认为这是同一个问题。

您似乎对触发该错误的
fone()
函数的输入有问题

尝试在该函数中执行
isset()
检查,以确保项目确实存在,然后再尝试返回它

例如

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

我已经有一段时间没有使用WordPress了,但是
$images
看起来超出了范围。我可能会尝试包装您的API工作,并在shortcode函数中引用它,如下所示。我会研究与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');
如果(!class_存在('MyAPI')){
类MyAPI
{
#创建图像存储
受保护的静态imgs;
#访问您的API
公共函数callInstagram($accesst='PUT YOUR INSTAGRAM ACCESS TOKEN HERE',$userid='YOUR INSTAGRAM USER ID HERE')
{
#如果已经使用内容设置了它,请返回它
如果(!empty(self::$imgs['instagram']))
返回自我::$imgs['instagram'];
//要获取的图像计数
$count=20;
//获取api内容
$content=file\u get\u contents('https://api.instagram.com/v1/users/self/media/recent/?access_token=“.$accesst.&count=”.$count);
//将JSON转换为对象
$standardres=json_decode($content,true);
//数组方法
foreach($standardres['data']作为$photo){
$imageData=base64_编码(文件获取内容($photo['images']['standard_resolution']['url']);
$images[]='';
}
#设置instagram图像存储
如果(!空($images))
#分配
self::$imgs['instagram]=$images;
#如果设置了,则返回图像
返回(isset(self:$imgs['instagram'])?self:$imgs['instagram']]:false;
}
#返回图像
公共函数getInstagramImg($img=false)
{
$imgs=$this->callInstagram();
如果($img!==false)
返回(isset($imgs[$img])?$imgs[$img]:false;
#全部归还
返回$imgs;
}
}
}
//为短代码创建函数
函数fone()
{
#创建API实例
$Instagram=新的MyAPI();
#发回列表中的第一个图像
返回$Instagram->getInstagramImg('0');
} 
//短代码
添加_短码('one','fone');

最后一点,我假设您的API工作是正确的,您应该先检查它是否工作,然后再开始疯狂地尝试找出
$images
不工作的原因。使用
print\r()
查看它是否从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');

?>


如果您想使用它,您可以随意使用,只需将“在此处放置您的访问令牌”和“在此处放置您的用户标识”替换为您的Instagram访问令牌和用户标识

正在以空格式发送某些内容。第29行返回$images[0];-这是第29行
$images
超出范围。这和它在同一个文件中写的完全一样吗?现在我什么也没有得到。那么它肯定与
$images
的值有关。这表明您正在向该函数传递某种类型的空对象/数组/字符串。如果Instagram不返回任何数据?
<?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');

?>