Home › Forums › Basel support forum › Instagram has returned invalid data
Instagram has returned invalid data
- This topic has 7 replies, 4 voices, and was last updated 6 years, 1 month ago by Artem Temos.
-
AuthorPosts
-
March 14, 2018 at 7:05 am #45816
ashridharParticipantHi,
The instagram widget is all of a sudden throwing this error.
“Instagram has returned invalid data”
I have not made any updates. It was working fine all this while. What could the issue be. Please help.March 14, 2018 at 8:19 am #45847
Artem TemosKeymasterHello,
Try to add the following PHP code snippet to the child theme functions.php file to do this
function basel_scrape_instagram( $username, $slice = 9 ) { $username = strtolower( $username ); $by_hashtag = ( substr( $username, 0, 1 ) == '#' ); $transient_name = 'instagram-media-new1-' . sanitize_title_with_dashes( $username ); $instagram = get_transient( $transient_name ); if ( false === $instagram ) { $request_param = ( $by_hashtag ) ? 'explore/tags/' . substr( $username, 1) : trim( $username ); $remote = wp_remote_get( 'https://instagram.com/'. $request_param ); if ( is_wp_error( $remote ) ) { return new WP_Error( 'site_down', esc_html__( 'Unable to communicate with Instagram.', 'basel' ) ); } if ( 200 != wp_remote_retrieve_response_code( $remote ) ) { return new WP_Error( 'invalid_response', esc_html__( 'Instagram did not return a 200.', 'basel' ) ); } $shards = explode( 'window._sharedData = ', $remote['body'] ); $insta_json = explode( ';</script>', $shards[1] ); $insta_array = json_decode( $insta_json[0], TRUE ); if ( ! $insta_array ){ return new WP_Error( 'bad_json', esc_html__( 'Instagram has returned invalid data.', 'basel' ) ); } if ( isset( $insta_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges'] ) ) { $images = $insta_array['entry_data']['ProfilePage'][0]['graphql']['user']['edge_owner_to_timeline_media']['edges']; } elseif( $by_hashtag && isset( $insta_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges'] ) ) { $images = $insta_array['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges']; } else { return new WP_Error( 'bad_json_2', esc_html__( 'Instagram has returned invalid data.', 'basel' ) ); } if ( ! is_array( $images ) ) { return new WP_Error( 'bad_array', esc_html__( 'Instagram has returned invalid data.', 'basel' ) ); } $instagram = array(); foreach ( $images as $image ) { $image = $image['node']; $caption = esc_html__( 'Instagram Image', 'basel' ); if ( ! empty( $image['edge_media_to_caption']['edges'][0]['node']['text'] ) ) $caption = $image['edge_media_to_caption']['edges'][0]['node']['text']; $image['thumbnail_src'] = preg_replace( "/^https:/i", "", $image['thumbnail_src'] ); $image['thumbnail'] = preg_replace( "/^https:/i", "", $image['thumbnail_resources'][0]['src'] ); $image['medium'] = preg_replace( "/^https:/i", "", $image['thumbnail_resources'][2]['src'] ); $image['large'] = $image['thumbnail_src']; $type = ( $image['is_video'] ) ? 'video' : 'image'; $instagram[] = array( 'description' => $caption, 'link' => '//instagram.com/p/' . $image['shortcode'], 'comments' => $image['edge_media_to_comment']['count'], 'likes' => $image['edge_liked_by']['count'], 'thumbnail' => $image['thumbnail'], 'medium' => $image['medium'], 'large' => $image['large'], 'type' => $type ); } // do not set an empty transient - should help catch private or empty accounts if ( ! empty( $instagram ) ) { $instagram = base64_encode( maybe_serialize( $instagram ) ); set_transient( $transient_name, $instagram, apply_filters( 'null_instagram_cache_time', HOUR_IN_SECONDS * 2 ) ); } } if ( ! empty( $instagram ) ) { $instagram = maybe_unserialize( base64_decode( $instagram ) ); return array_slice( $instagram, 0, $slice ); } else { return new WP_Error( 'no_images', esc_html__( 'Instagram did not return any images.', 'basel' ) ); } }
Regards
March 14, 2018 at 9:47 am #45878
BioterrariumParticipantThanks I had the same problem too I added your snippet and Instagram is back to work properly.
Regards
March 14, 2018 at 10:52 am #45905
Artem TemosKeymasterGreat, we are glad that it helped you.
March 14, 2018 at 2:34 pm #45973
heartitParticipantHi Artem,
We get the following error when adding that PHP snippet to fix the Instagram issue:
Don’t Panic
The code snippet you are trying to save produced a fatal error on line 78:Cannot redeclare basel_scrape_instagram() (previously declared in /home/heartitd/public_html/monzacarcare.com/wp-content/themes/basel/inc/shortcodes.php:238)
The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.
Any help appreciated.
Cheers
March 14, 2018 at 3:13 pm #46006
Artem TemosKeymasterYou need to edit your child theme’s functions.php file with FTP, not via the Dashboard.
March 14, 2018 at 3:30 pm #46019
heartitParticipantPerfect, cheers!
March 14, 2018 at 3:44 pm #46023
Artem TemosKeymasterYou are welcome!
-
AuthorPosts
Tagged: Instagram Widget Error
The topic ‘Instagram has returned invalid data’ is closed to new replies.
- You must be logged in to create new topics. Login / Register