Reputation: 21
I made a php file for find and return an Instagram post likes count. I uploaded this php file on api.mydomain.com. This is the codes:
<?php
$url = $_GET["url"];//Instagram Post Link
$resp = file_get_contents("$url?__a=1");
$r = json_decode($resp,true);
if (strpos($resp, 'edge_media_preview_like') !== false) {
$start_counter = $r['graphql']['shortcode_media']['edge_media_preview_like']['count'];
}else{
$start_counter="ERROR";
}
return $start_counter;
When I send request with my browser myself, it return the likes count. But when I send request to api.mydomain.com with the following codes that uploaded on my host, I receive null.
<?php
$InstagramPost = "https://www.instagram.com/p/CETxYxtFq8h/"; //an Instagram post for example
$API_LINK = "http://api.mydomain.com?url=$InstagramPost";
$resp = file_get_contents($API_LINK);
echo $resp;
Why does this problem arise?
Upvotes: 2
Views: 205
Reputation: 1647
First of all you should always validate input variable. In your case you should validate $_GET["url"]
because anyone could use your api.mydomain.com for bad things, f.e. DoS.
Next thing is that you should escape an url before inserting it inside the other url:
$InstagramPost = "https://www.instagram.com/p/CETxYxtFq8h/";
$instaEscaped = rawurlencode($InstagramPost);
$API_LINK = "http://api.mydomain.com?url=$instaEscaped";
And last this is that before doing json_decode
you'd better check given response. Maybe it's already false
:
$url = $_GET["url"];
$resp = file_get_contents("$url?__a=1");
if (false === $resp) {
// do something
}
$r = json_decode($resp,true);
Upvotes: 1