Skip to content

Commit

Permalink
Fixed error while embedding non-existant Instagram post
Browse files Browse the repository at this point in the history
  • Loading branch information
AmauryCarrade committed Aug 23, 2018
1 parent 065a56a commit d3c145a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
2 changes: 1 addition & 1 deletion blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Static Social Embeds
version: 1.1.2
version: 1.1.3
description: |
Embeds social status (like tweets, instagram posts, toots, etc.) in articles without using their embed iframe,
but rather statically without any dependency to the service.
Expand Down
12 changes: 11 additions & 1 deletion shortcodes/InstagramShortcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,27 @@ protected function getData($url)
]);

$raw_instagram_html = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

$error_code = curl_errno($ch);
$error = $error_code != 0 ? (': #' . $error_code . ' - ' . curl_error($ch)) : '';

if (!$error && $http_code != 200)
{
$error = ': HTTP ' . $http_code . ($http_code == 404 ? ' - Not Found' : '');
$raw_instagram_html = null;
}

curl_close($ch);

if (!$raw_instagram_html)
return ['errors' => [['code' => 0, 'message' => 'Unable to retrieve instagram post' . $error]], 'url' => $url];

preg_match('/window\._sharedData = (.*);<\/script>/', $raw_instagram_html, $matches, PREG_OFFSET_CAPTURE, 0);

if (!$matches || count($matches) < 2 || count($matches[1]) < 1)
return ['errors' => [['code' => 0, 'message' => 'Unable to retrieve instagram post: cannot parse the web page to retrieve data.']], 'url' => $url];

$post = json_decode($matches[1][0], true);

if (!$post
Expand All @@ -74,7 +84,7 @@ protected function getData($url)
|| !isset($post['entry_data']['PostPage'][0]['graphql'])
|| !isset($post['entry_data']['PostPage'][0]['graphql']['shortcode_media']))
{
return ['errors' => [['code' => 0, 'message' => 'Unable to retrieve instagram post']], 'url' => $url];
return ['errors' => [['code' => 0, 'message' => 'Unable to retrieve instagram post: cannot parse the web page to retrieve data.']], 'url' => $url];
}

// Instagram Post or Inner Post (as you like)
Expand Down

0 comments on commit d3c145a

Please sign in to comment.