Instagram Story Scraper - php

i built a Instagram Story Scraper, but its not working...
I've tried everything, but there's something wrong with my code
I hope you can help me
this is my index.php
<?php
require_once('instagramStory.php');
$story = new instagram_story();
echo $story->getStory("garyvee");
?>
and this is my instagramStory.php
<?php
class instagram_story{
protected function file_get_contents_curl($url){
$cookies = dirname(__FILE__)."/cookie.txt" ;
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($curl, CURLOPT_COOKIEFILE, $cookies);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
$answer = curl_exec($curl);
curl_close($curl);
return $answer;
}
public function getStory($username){
$url = file_get_contents("https://www.instagram.com/$username/");
$json = '/sharedData\s=\s(.*[^\"]);<.script>/ixU';
preg_match_all($json, $url, $jsondata, PREG_SET_ORDER, 0);
$array = json_decode($jsondata[0][1], true);
$user_id = $array['entry_data']['ProfilePage']['0']['graphql']['user']['id'];
$stories = $this->file_get_contents_curl("https://www.instagram.com/graphql/query/?query_hash=de8017ee0a7c9c45ec4260733d81ea31&variables=%7B%22reel_ids%22%3A%5B%22$user_id%22%5D%2C%22tag_names%22%3A%5B%5D%2C%22location_ids%22%3A%5B%5D%2C%22highlight_reel_ids%22%3A%5B%5D%2C%22precomposed_overlay%22%3Afalse%2C%22show_story_viewer_list%22%3Atrue%2C%22story_viewer_fetch_count%22%3A50%2C%22story_viewer_cursor%22%3A%22%22%7D");
$data = json_decode($stories, true);
var_dump($data); die();
$_story = [];
foreach ($stories as $story) {
$vid = 'video_resources';
if (!array_key_exists($vid, $story)) {
$_story [] = $story['display_url'];
} else {
$_story [] = $story['video_resources'][0]['src'];
}
}
foreach ($_story as $story) {
$check = '/mp4/m';
preg_match_all($check, $story, $matches, PREG_SET_ORDER, 0);
if (empty($matches)) {
echo "<a href=$story&dl=1><img src=$story></a>";
} else {
echo '<video width="320" height="240" controls>';
echo '<source src="' . $story . '" type="video/mp4">';
echo '</video>';
echo "<a href=$story&dl=1>Download</a>";
}
}
}
}
?>
this is the error code I get

Related

PHP: JSON Api Tenor

How can I get all url-s from https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8 (results->media->nanomp4->url)
if($json = cURLGetContents("https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8")) {
$obj = json_decode($json);
echo $obj->results->{"media"}->{"nanomp4"}->{"url"};
}
Function cURLGetContents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
On a circulate basis:
$json = cURLGetContents("https://api.tenor.com/v1/trending?key=LIVDSRZULELA&limit=8");
$urlList = getAllUrls((array) json_decode($json, true));
function getAllUrls($input, $list = []) {
foreach ($input as $key => $data) {
if (is_array($data)) {
$list = getAllUrls($data, $list);
} elseif ($key === 'url') {
$list[] = $data;
}
}
return $list;
}
$endereco = "https://api.tenor.com/v1/search?key=MBDPHCT6LA4H&q=sexo&limit=2";
$GrabURL = cURLGetContents($endereco);
$searchResponse = json_decode($GrabURL, true);
foreach ($searchResponse["results"] as $searchResult) {
print_r($searchResult["media"][0]["tinygif"]["url"]);
}
function cURLGetContents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}

How to Properly Convert String to Associative Array in PHP

i want to convert this string to Proper associative array.
please click on the link to see string data.
https://maps.googleapis.com/maps/api/geocode/json?latlng=16.539311299999998,80.5820222
i want that to be converted into a proper Associaitve array.
im looking something like this.
echo $array['long_name'] should return a value like 'Attlee Road'
please help me with this, im trying this from 2 days, trying all explode, implode, foreachloops, im confused a lot, please someone help me with this.
Below code will echo the string data
<?php
$lat = 16.539311299999998;
$lng = 80.5820222;
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $lng . ";
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_contents = get_data($url);
echo "<pre>";
print_r($returned_contents);
echo "</pre>";
?>
You must be use json_decode() function.
<?php
$lat = 16.539311299999998;
$lng = 80.5820222;
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $lng . "&key=AIzaSyDCgyjLTrpadabEAyDNXf2GPpgChvpMw_4";
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_contents = get_data($url);
$returned_contents_array = json_decode($returned_contents, true);
$results = $returned_contents_array['results'];
foreach($results as $result) {
echo $result['address_components'][0]['long_name'];
}
?>
Use json_decode() on your output
Try it
<?php
$lat = 16.539311299999998;
$lng = 80.5820222;
$url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" . $lat . "," . $lng . "&key=AIzaSyDCgyjLTrpadabEAyDNXf2GPpgChvpMw_4";
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_contents = get_data($url);
$returned_contents = json_decode($returned_contents);
echo "<pre>";
print_r($returned_contents);
echo "</pre>";
?>

Dealing with curl to get count for tweets containing that url

I was using following code to decode the tinyurl from twitter:
function MyURLDecode($url)
{
$ch = #curl_init($url);
#curl_setopt($ch, CURLOPT_HEADER, TRUE);
#curl_setopt($ch, CURLOPT_NOBODY, TRUE);
#curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
#curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$url_resp = #curl_exec($ch);
preg_match('/Location:\s+(.*)\n/i', $url_resp, $i);
if (!isset($i[1]))
{
return $url;
}
return $i[1];
}
$url = MyURLDecode($url);
This using this result to get tweet count value:
function get_twitter_url_count($url) {
$encoded_url = urlencode($url);
$content = #file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $encoded_url);
return $content ? json_decode($content)->count : 0;
}
Above code works fine. But due to some issue I need to change the Url Decoding function which is as below:
function TextAfterTag($input, $tag)
{
$result = '';
$tagPos = strpos($input, $tag);
if (!($tagPos === false))
{
$length = strlen($input);
$substrLength = $length - $tagPos + 1;
$result = substr($input, $tagPos + 1, $substrLength);
}
return trim($result);
}
function expandUrlLongApi($url)
{
$format = 'json';
$api_query = "http://api.longurl.org/v2/expand?" .
"url={$url}&response-code=1&format={$format}";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $api_query );
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
$fileContents = curl_exec($ch);
curl_close($ch);
$s1=str_replace("{"," ","$fileContents");
$s2=str_replace("}"," ","$s1");
$s2=trim($s2);
$s3=array();
$s3=explode(",",$s2);
$s4=TextAfterTag($s3[0],(':'));
$s4=stripslashes($s4);
return $s4;
}
This code gives decoded url correctly, infact with better accuracy then previous one but now result of this does not works with this function:
function get_twitter_url_count($url) {
$encoded_url = urlencode($url);
$content = #file_get_contents('http://urls.api.twitter.com/1/urls/count.json?url=' . $encoded_url);
return $content ? json_decode($content)->count : 0;
}
Rather givig correct count value, it always gives 0.
Can someone tell me what is wrong here?
full code:
<?php
function TextAfterTag($input, $tag)
{
$result = '';
$tagPos = strpos($input, $tag);
if (!($tagPos === false))
{
$length = strlen($input);
$substrLength = $length - $tagPos + 1;
$result = substr($input, $tagPos + 1, $substrLength);
}
return trim($result);
}
function expandUrlLongApi($url)
{
$format = 'json';
$api_query = "http://api.longurl.org/v2/expand?" .
"url={$url}&response-code=1&format={$format}";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $api_query );
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($ch, CURLOPT_HEADER, false);
$fileContents = curl_exec($ch);
/* preg_match('/Location:\s+(.*)\n/i', $fileContents, $i);
if (!isset($i[1]))
{
return '';
}
else
$fileContents = $i[1]; */
curl_close($ch);
$s1=str_replace("{"," ","$fileContents");
$s2=str_replace("}"," ","$s1");
$s2=trim($s2);
$s3=array();
$s3=explode(",",$s2);
$s4=TextAfterTag($s3[0],(':'));
$s4=stripslashes($s4);
return $s4;
}
$url = expandUrlLongApi('http://t.co/SFdM0wjtGA');
echo "Url is : $url <br/>";
$url = get_twitter_url_count($url);
?>

How to extract multiple data from google maps in using php and curl?

Here is a simple code I'm trying to run but it does not out put any data. Could someone help? Thanks!!
<?php
$addr = "Hotels in ottawa canada";
$a = urlencode($addr);
$geocodeURL =
"https://maps.googleapis.com/maps/api/geocode/json?address=$a&sensor=false&key=my key";
$ch = curl_multi_init($geocodeURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_multi_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$geocode = json_decode($result);
$lat = $geocode->results[$i]->geometry->location->lat;
$lng = $geocode->results[$i]->geometry->location->lng;
echo $formatted_address = $geocode->results[$i]->formatted_address;
echo $geo_status = $geocode->status;
echo $location_type = $geocode->results[0]->geometry->location_type;
echo $location_type = $geocode->results[$i]->geometry->premise;
echo $street_address = $geocode->results[$i]->street_address;
?>
Social Classifieds
If your looking to pass multiple addresses in parallel to the google api and then parse the results as one, ive put together a little example that does just that and fixed your curl multi code. Hope it helps.
<?php
$urls = array(
'http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true',
'http://maps.googleapis.com/maps/api/geocode/json?address='.urlencode('Hotels').'&sensor=true');
$response = curl_get_multi($urls);
//Handle response array
foreach($response as $json){
$result = json_decode($json);
//echo $result->status.' ';
//Iterate each result
foreach($result->results as $res){
//lat
echo $res->geometry->location->lat.' ';
//lng
echo $res->geometry->location->lng.' ';
//formatted_address
echo $res->formatted_address.' ';
//location_type
echo $res->geometry->location_type.' ';
echo '<br/>';
}
echo '<hr />';
}
/**
* Curl multi function
*
* #param Array $urls
* #return Array
*/
function curl_get_multi($urls) {
$curly = array();
$result = array();
$mh = curl_multi_init();
foreach ($urls as $id=>$url) {
$curly[$id] = curl_init();
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curly[$id], CURLOPT_TIMEOUT, 2);
curl_setopt($curly[$id], CURLOPT_USERAGENT, 'CurlRequest');
curl_setopt($curly[$id], CURLOPT_REFERER, $url);
curl_setopt($curly[$id], CURLOPT_AUTOREFERER, true);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh, $curly[$id]);
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
curl_multi_close($mh);
return $result;
}
?>

foreach arrays?

Script:
include_once 'simple_html_dom.php';
$ckfile = 'cookie.txt';
foreach (range('a', 'z') as $letters) {
echo $letters;
}
foreach (range('1', '100') as $numbers) {
echo $numbers;
}
$ch = curl_init ("http://site/test.php?letter=".$letters."&page=".$numbers."");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec ($ch);
$html = str_get_html(''.$output.'');
foreach($html->find('a') as $element) {
echo $element->href . '<br>';
}
As you can see there is three arrays seperated, one by one works, but how them 'put together' in one foreach and do 'job' ?
Etc. I'm not familiar with foreach after all, and if i remember correctly, it's my second time with 'them'...
foreach (array_merge($arr1, $arr2, $arr3) as element) { /* do something */ }
array_merge()
include_once 'simple_html_dom.php';
$ckfile = 'cookie.txt';
foreach (range('a', 'z') as $letters) {
echo $letters;
foreach (range('1', '100') as $numbers) {
echo $numbers;
$ch = curl_init ("http://site/test.php?letter=".$letters."&page=".$numbers."");
curl_setopt ($ch, CURLOPT_COOKIEFILE, $ckfile);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output = curl_exec ($ch);
$html = str_get_html(''.$output.'');
foreach($html->find('a') as $element) {
echo $element->href . '<br>';
}
}
echo $numbers;
}
But this script will execute for a few minutes... What do you need it for?

Categories