Skip to content

Commit

Permalink
Fixes #47. Image URLS broken for some medias in getMediaByCode()
Browse files Browse the repository at this point in the history
  • Loading branch information
raiym committed Dec 2, 2016
1 parent 88e6965 commit 2b8d4e8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 50 deletions.
38 changes: 3 additions & 35 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,19 @@

require_once 'vendor/autoload.php';
require_once 'src/InstagramScraper.php';
use InstagramScraper\Exception\InstagramException;

use InstagramScraper\Instagram;

$instagram = new Instagram();
try {
// $medias = Instagram::getMedias('kevin', 1497);
$parameters = 'ig_user(3){id,username,external_url,full_name,profile_pic_url,biography,followed_by{count},follows{count},media{count},is_private,is_verified}';
// echo json_encode($medias[1497]);
$account = json_decode(getContentsFromUrl($parameters), ($assoc || $assoc == "array"));
print_r($account);
$media = Instagram::getMediaByCode('BL0k1EXhElI');
echo json_encode($media);
} catch (\Exception $ex) {
print_r($ex);
}


function getContentsFromUrl($parameters) {
if (!function_exists('curl_init')) {
return false;
}
$random = generateRandomString();
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.instagram.com/query/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'q='.$parameters);
$headers = array();
$headers[] = "Cookie: csrftoken=$random;";
$headers[] = "X-Csrftoken: $random";
$headers[] = "Referer: https://www.instagram.com/";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}

function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}

//echo Media::getIdFromCode('z-arAqi4DP') . '<br/>';
//echo Media::getCodeFromId('936303077400215759_123123');
//echo Media::getLinkFromId('936303077400215759_123123');
Expand Down
1 change: 1 addition & 0 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Endpoints
const LAST_LIKES_BY_CODE = 'ig_shortcode({{code}}){likes{nodes{id,user{id,profile_pic_url,username,follows{count},followed_by{count},biography,full_name,media{count},is_private,external_url,is_verified}},page_info}}';

const INSTAGRAM_QUERY_URL = 'https://www.instagram.com/query/';
const INSTAGRAM_CDN_URL = 'https://scontent.cdninstagram.com/';

public static function getAccountPageLink($username)
{
Expand Down
20 changes: 5 additions & 15 deletions src/InstagramScraper/Model/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,13 @@ public static function fromMediaPage($mediaArray)

private static function getImageUrls($imageUrl)
{
$imageUrl = self::getCleanImageUrl($imageUrl);
$parts = explode('/', parse_url($imageUrl)['path']);
if (sizeof($parts) == 4) {
$standard = 'https://scontent.cdninstagram.com/' . $parts[1] . '/s640x640/' . $parts[2] . '/' . $parts[3];
} else {
if (isset($parts[4]) && $parts[4][0] == 'p') {
$standard = 'https://scontent.cdninstagram.com/' . $parts[1] . '/p640x640/' . $parts[3] . '/' . $parts[4];
} else {
$standard = 'https://scontent.cdninstagram.com/' . $parts[1] . '/s640x640/' . $parts[3] . '/' . $parts[4];
}
}

$imageName = $parts[sizeof($parts) - 1];
$urls = [
'standard' => $standard,
'low' => str_replace('640x640', '320x320', $standard),
'high' => str_replace('640x640', '1080x1080', $standard),
'thumbnail' => str_replace('640x640', '150x150', $standard)
'standard' => Endpoints::INSTAGRAM_CDN_URL . 't/s640x640/' . $imageName,
'low' => Endpoints::INSTAGRAM_CDN_URL . 't/s320x320/' . $imageName,
'high' => Endpoints::INSTAGRAM_CDN_URL . 't/' . $imageName,
'thumbnail' => Endpoints::INSTAGRAM_CDN_URL . 't/s150x150/' . $imageName
];
return $urls;
}
Expand Down

0 comments on commit 2b8d4e8

Please sign in to comment.