Skip to content

Instantly share code, notes, and snippets.

@cyb3rsalih
Last active October 19, 2018 04:31
Show Gist options
  • Save cyb3rsalih/01bd2897a3bf748156abe385c03be6f7 to your computer and use it in GitHub Desktop.
Save cyb3rsalih/01bd2897a3bf748156abe385c03be6f7 to your computer and use it in GitHub Desktop.
Read JSON Object without API
<h1 class="follower"></h1>
<?php
$instagramUsername = "instagram";
// Get Instagram page source code
$instagram = file_get_contents('http://instagram.com/'.$instagramUsername);
// extract follower count from json with regex
$pattern = '/\"edge_followed_by\":\{\"count\":[0-9]{1,10}\}/';
preg_match_all($pattern, $instagram, $results);
$string = print_r($results[0][0],true);
// Transform result to JSON type
$myJsonObject = "{".$string."}";
?>
<script>
// Transfer PHP variable to Javascript
var js_Obj = '<?php echo $myJsonObject;?>'
// String to JSON object
obj = JSON.parse(js_Obj);
console.log(obj.edge_followed_by);
// Prepare and send to HTML
document.getElementById("follower").innerHTML = JSON.stringify(obj.edge_followed_by["count"]);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment