2017-09-05 2 views
3

Ich bin zu einigen Problemen gekommen. Ich habe ein Wordpress-Plugin erstellt, das automatisch die letzten 20 Instagram-Posts erhält und es sollte mir theoretisch erlauben, das neueste Bild als Shortcode in den Post einzufügen. Nun wird der Code, dies zu reproduzieren ist:PHP Fehler Absturz Code

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

Grundsätzlich Ich erhalte eine Fehlermeldung angezeigt wird:

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

Irgendwelche Ideen wie diese zu lösen? A var_dump() gibt mir die Bilder über der Kopfzeile .. Und bitte zeigen Sie mich nicht auf Unitylased String Offset Thread, weil ich es nicht wirklich als das gleiche Problem sehe.

+0

Etwas wird leer gesendet. – clearshot66

+0

welche Linie 29 ist – Juan

+0

zurück $ images [0]; - Dies ist Zeile 29 –

Antwort

1

Ich habe Wordpress für eine Weile nicht benutzt, aber $images schaut aus Anwendungsbereich. Ich würde vielleicht versuchen, Ihre API-Arbeit zu verpacken und sie innerhalb der Shortcode-Funktion zu referenzieren, etwas wie unten. Ich würde die Best Practice auf diese Art der Sache zu Wordpress-bezogene Forschung:

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

Eine letzte Anmerkung, ich gehe davon aus, dass Ihre API Arbeit korrekt ist, sollten Sie prüfen, ob es funktioniert, bevor Sie beginnen alle verrückt zu werden versuchen, herauszufinden, warum die $images nicht funktioniert. Verwenden Sie print_r(), um festzustellen, ob es die richtigen Informationen von Instagram zurückgibt.

0

Es scheint, Sie haben ein Problem mit der Eingabe in die fone()-Funktion, die diesen Fehler auslöst.

Versuchen Sie, eine isset() Überprüfung innerhalb dieser Funktion durchzuführen, um sicherzustellen, dass das Element tatsächlich existiert, bevor Sie versuchen, es zurückzugeben.

Beispiel ...

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

Jetzt bekomme ich nichts. –

+0

Dann hängt es definitiv mit dem Wert von $ images zusammen. Dies würde bedeuten, dass Sie ein leeres Objekt/Array/String von irgendeiner Art in diese Funktion übergeben. Vielleicht, wenn Instagram keine Daten zurückgibt? – neuromatter

1

Initialisieren Sie $images, bevor Sie die Werte durchlaufen und anhängen, insbesondere, da Sie sie als Parameter übergeben möchten. Vor der foreach Schleife hinzu:

$images = array(); 
0

Vielen Dank für die Hilfe Jungs, wenn Sie den gesamten Code in Arbeitszustand sehen wollen:

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

?> 

Wenn Sie möchten, es zu benutzen, Sie Dafür kannst du einfach "PUT_YOUR_ACCESS_TOKEN_HERE" und "PUT_YOUR_USER_ID_HERE" mit deinem Instagram Access Token und deiner Benutzer ID ersetzen!