Skip to content

Custom Params

Justin Stolpe edited this page May 21, 2022 · 8 revisions

The Instagram Graph API PHP SDK allows for fully customizable request. By default, the SDK tries to return all possible fields and parameters. If, for whatever reason, you want to make your own request or specify certain parameters, this page is for you.

Specifying Parameters

By default the getSelf() will ask for and return all possible parameters and fields for an endpoint. However, you can pass an array into any getSelf() function call to specify only certain parameters. The key value pair in the custom params passed in must match the required key values expected by the Instagram Graph API. Let's look at business discovery as an example.

Code - Default

/**
 * SDK defaults to returning all fields for any endpoint if you don't pass in any params.
 * Fields returned by default 'business_discovery.username(<USERNAME>){username,website,name,ig_id,id,profile_picture_url,biography,follows_count,followers_count,media_count,media{id,username,caption,like_count,comments_count,timestamp,media_product_type,media_type,owner,permalink,media_url,children{id,media_type,media_url}}}'
 */
$userBusinessDiscovery = $businessDiscovery->getSelf();

Code - Custom Params

/**
 * Custom params array structure is very important. The key must match what Instagram Graph API 
 * is expecting as the parameter name. The value must also match what the Instagram Graph API 
 * is expecting for the given key.
 * 
 * Example Instagram Graph API "https://url-to-endpoint/?param1=value1&param2=value2"
 * To override default values with the sdk, your custom params array you pass to getSelf() would look like this:
 *     $params = array(
 *         'param1' => 'value1',
 *         'param2' => 'value2'
 *     );
 */

/**
 * Here is a real example call for business discovery getSelf(). Instead of defaulting to return all the fields,
 * we are only requesting the username and profile picture url. With this custom params array, when the SDK makes
 * the call to the endpoint, it would look like this:
 *
 * "https://url-to-endpoint/?fields=business_discovery.username(<USERNAME>){username,profile_picture_url}"
 */
$params = array( // key value pairs must match what the Instagram Graph API expects for the endpoint you are hitting
    'fields' => 'business_discovery.username(<USERNAME>){username,profile_picture_url}'
);
$userBusinessDiscovery = $businessDiscovery->getSelf( $params );
Clone this wiki locally