Using serpapi.com Search API to get Instagram results - PHP Example

A simple example showing how to get a user's Instagram photo posts via serpapi.com API client interface.


// PHP Code Example

// First step, require the lib

<?php

        require 'google-search-results.php';

require 'restclient.php';

 // Connect to the API

$client = new GoogleSearch("API KEY GOES HERE");

 // Capture and set Instagram username via Get request

        $profile_id = str_replace(" ","",$_GET['username']);

$query = ["profile_id" => $profile_id,"engine" => "instagram_profile"];

 // Query the API client for results

$response = $client->get_json($query);

$instagram_results = array();

if($response->profile_results->posts != null){

                $i = 1;

// If results, Loop the posts into an array

foreach($response->profile_results->posts as $photo){

// Limit results — set higher for more results

    if($i == 5){break;}

    array_push($instagram_results,$photo);

                    $i++;

}

}

$data = json_encode($instagram_results);

// Echo out JSON data

echo '{"items":'.$data.',"error":0,"pg":0}';

?>

Comments