@@ -48,19 +48,33 @@ protected function getData($url)
4848 curl_setopt_array ($ ch , [
4949 CURLOPT_TIMEOUT => 3600 ,
5050 CURLOPT_URL => $ url ,
51+ CURLOPT_FOLLOWLOCATION => true ,
5152 CURLOPT_RETURNTRANSFER => true ,
5253 CURLOPT_SSL_VERIFYPEER => false
5354 ]);
5455
5556 $ raw_instagram_html = curl_exec ($ ch );
57+ $ http_code = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
58+
59+ $ error_code = curl_errno ($ ch );
60+ $ error = $ error_code != 0 ? (': # ' . $ error_code . ' - ' . curl_error ($ ch )) : '' ;
61+
62+ if (!$ error && $ http_code != 200 )
63+ {
64+ $ error = ': HTTP ' . $ http_code . ($ http_code == 404 ? ' - Not Found ' : '' );
65+ $ raw_instagram_html = null ;
66+ }
5667
5768 curl_close ($ ch );
5869
5970 if (!$ raw_instagram_html )
60- return ['errors ' => [['code ' => 0 , 'message ' => 'Unable to retrieve instagram post ' ]], 'url ' => $ url ];
71+ return ['errors ' => [['code ' => 0 , 'message ' => 'Unable to retrieve instagram post ' . $ error ]], 'url ' => $ url ];
6172
6273 preg_match ('/window\._sharedData = (.*);<\/script>/ ' , $ raw_instagram_html , $ matches , PREG_OFFSET_CAPTURE , 0 );
6374
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+
6478 $ post = json_decode ($ matches [1 ][0 ], true );
6579
6680 if (!$ post
@@ -70,7 +84,7 @@ protected function getData($url)
7084 || !isset ($ post ['entry_data ' ]['PostPage ' ][0 ]['graphql ' ])
7185 || !isset ($ post ['entry_data ' ]['PostPage ' ][0 ]['graphql ' ]['shortcode_media ' ]))
7286 {
73- 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 ];
7488 }
7589
7690 // Instagram Post or Inner Post (as you like)
0 commit comments