Gif Images - Giphy Api PHP

1

Hello stack overflow people I have a problem with this Api of Giphy what happens is that I do not get the image that I'm pining but it does not give me any good error I hope you help me see my error

<?php

    $url = file_get_contents('http://api.giphy.com/v1/gifs/search?q=cat&api_key=dc6zaTOxFJmzC&limit=7');
    $json = json_decode($url);
    $gif = @$json->data->images->original->url;

        echo '<img src="'.$gif.'" style="width: 100%; position: relative;">';
?>

link

    
asked by Shareiv 08.09.2017 в 18:08
source

1 answer

2

If you want to collect only 1 giphy with a specific id you have to change the URL to:

http://api.giphy.com/v1/gifs/gif_id?api_key=api_key

It would be then:

$url  = file_get_contents( 'http://api.giphy.com/v1/gifs/vFKqnCdLPNOKc?api_key=dc6zaTOxFJmzC' );
$json = json_decode( $url );

if ( isset( $json->data->images->original->url ) ) {

    echo "<img src='{$json->data->images->original->url}' width='100' height='100'>";
}

Demo Single Giphy

If you want to collect several giphys with category cat and limit 7 , it would be like this:

$url  = file_get_contents( 'http://api.giphy.com/v1/gifs/search?q=cat&api_key=dc6zaTOxFJmzC&limit=7' );
$json = json_decode( $url );

foreach ( $json as $values ) {

    foreach ( $values as $value ) {

        if ( isset( $value->images->original->url ) ) {

            echo "<img src='{$value->images->original->url}' width='100' height='100'>";
        }
    }
}

Demo Multi Giphy

    
answered by 08.09.2017 / 18:37
source