Skip to content

Commit d3c145a

Browse files
committed
Fixed error while embedding non-existant Instagram post
1 parent 065a56a commit d3c145a

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

blueprints.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: Static Social Embeds
2-
version: 1.1.2
2+
version: 1.1.3
33
description: |
44
Embeds social status (like tweets, instagram posts, toots, etc.) in articles without using their embed iframe,
55
but rather statically without any dependency to the service.

shortcodes/InstagramShortcode.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,27 @@ protected function getData($url)
5454
]);
5555

5656
$raw_instagram_html = curl_exec($ch);
57+
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
5758

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

62+
if (!$error && $http_code != 200)
63+
{
64+
$error = ': HTTP ' . $http_code . ($http_code == 404 ? ' - Not Found' : '');
65+
$raw_instagram_html = null;
66+
}
67+
6168
curl_close($ch);
6269

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

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

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

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

8090
// Instagram Post or Inner Post (as you like)

0 commit comments

Comments
 (0)