【问题标题】:PHP error crashing codePHP错误崩溃代码
【发布时间】:2017-09-05 18:08:56
【问题描述】:

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

【问题讨论】:

  • 发送的东西是空的。
  • 第 29 行
  • return $images[0]; - 这是第 29 行
  • $images 出现在范围之外。这与写在同一个文件中的完全一样吗?

标签: php wordpress crash instagram-api


【解决方案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 返回了正确的信息。

【讨论】:

    【解决方案2】:

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

    $images = array();
    

    【讨论】:

      【解决方案3】:

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

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

      示例...

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

      【讨论】:

      • 现在我什么也没得到。
      • 那么肯定和$images的值有关。这表明您正在将某种空对象/数组/字符串传递给该函数。也许如果 Instagram 不返回任何数据?
      【解决方案4】:

      感谢大家的帮助,请检查是否要查看整个代码处于工作状态:

      <?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!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-25
        • 1970-01-01
        • 2013-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-04
        相关资源
        最近更新 更多