• Resolved sarahjadesigns

    (@sarahjadesigns)


    I saw a warning on my dashboard about how I’m using an insecure version of PHP (5.6) and should upgrade to at least 7.4. When I did, I got PHP errors all over my site, but most were fairly easy to fix – mostly some single quotes missing.

    There’s one I haven’t been able to crack. My site uses both Facebook and WP comments, so I have a function to consolidate and display the total count. This is the error I’m getting at the bottom of each post where this should display:

    Warning: file_get_contents('https://graph.facebook.com/?ids=' . http://lucky-stars.ca/ptp/oatmeal-squares/): failed to open stream: No such file or directory in /home/luckysta/public_html/wp/wp-content/themes/yum/functions.php on line 513

    The relevant snippet of code from functions.php follows:

    function full_comment_count() {  
    global $post;  
    $url = get_permalink($post->ID);  
          
    $filecontent = file_get_contents('https://graph.facebook.com/?ids=' . $url); // This is line 513 referred to in the error
    $json = json_decode($filecontent);  
    $count = $json->$url->comments;  
    $wpCount = get_comments_number();  
    $realCount = $count + $wpCount;  
    if ($realCount == 0 || !isset($realCount)) {  
        $realCount = 0;  
    }  
    return $realCount;  
    }

    Does anyone know what the issue is here? I’m guessing it’s just some incorrect nomenclature/formatting of PHP or somesuch, but my knowledge of this coding language is very limited. Note that I reverted back to PHP 5.6 while I troubleshoot this, so the error with not currently be visible on my site.

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • It seems like it’s the configuration of your PHP. The 5.6 version is set to allow fopen wrappers and 7.4 is not? Read https://www.php.net/manual/en/function.file-get-contents.php

    Thread Starter sarahjadesigns

    (@sarahjadesigns)

    I’m not sure what you mean @joyously, the info at the top of the page you linked seems to indicate it works with versions 5, 7 and 8. Even the changelog further down indicates as much.

    But if I’m misinterpreting this, then what could I do to replace file_get_contents()?

    Read the entire page, including the Note at the bottom which talks about fopen wrappers.

    Thread Starter sarahjadesigns

    (@sarahjadesigns)

    Thanks @joyously, I did read it. However, this bit and the supporting links about fopen wrappers are sort of Greek to me, as I don’t know enough about PHP to code it from scratch. Is the notion that I can just swap out file_get_contents() with fopen(), or is there more I need to do here?

    No, you shouldn’t need to change any PHP. You need to investigate the settings on your server to discover the difference between the two versions. You might have to ask your host, because they can have settings to turn off the fopen wrappers that your code relies on.

    Thread Starter sarahjadesigns

    (@sarahjadesigns)

    Thanks for clarifying. I did have a look at the extensions in my cPanel, but didn’t find anything similar to zlib, which is why I thought I maybe had initially misunderstood your link. I’ll get in touch with my host and see if they can help me get to the bottom of this.

    Thread Starter sarahjadesigns

    (@sarahjadesigns)

    Looks like zlib was enabled on both with PHP 5.6 and 7.4. I tried switching to version 8.0 to see if whatever issue had been resolved with that one, but that just gives me a big fat WordPress error and the site won’t load at all.

    I’m completely at a loss for how to fix this. Any other insight would be appreciated.

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    @sarahjadesigns You actually need to rewrite it and use the HTTP API.

    In this case, you probably need https://developer.wordpress.org/reference/functions/wp_remote_get/ to get the data.

    A number of hosts block getting file contents like that one external (non local) files, as a security measure.

    Thread Starter sarahjadesigns

    (@sarahjadesigns)

    Thanks @ipstenu. My skills with PHP pretty much only extend to be able to tweak things a tiny bit. When it comes to function building, I have a pretty fuzzy understanding.

    Comparing your info on wp_remote_get() and file_get_contents(), I get the sense that the main difference is that the former allows you to fetch an external URL. Could I then just swap it like so?

    function full_comment_count() {  
    global $post;  
    $url = get_permalink($post->ID);  
          
    $filecontent = wp_remote_get('https://graph.facebook.com/?ids=' . $url);
    $json = json_decode($filecontent);  
    $count = $json->$url->comments;  
    $wpCount = get_comments_number();  
    $realCount = $count + $wpCount;  
    if ($realCount == 0 || !isset($realCount)) {  
        $realCount = 0;  
    }  
    return $realCount;  
    }

    Or is there more I need to do with wp_remote_get() to get a combined count of Facebook and WordPress comments for a post?

    Moderator Ipstenu (Mika Epstein)

    (@ipstenu)

    🏳️‍🌈 Advisor and Activist

    Something like that. The details depends on Facebook and how it returns content. I haven’t looked deeply at that part.

    Thread Starter sarahjadesigns

    (@sarahjadesigns)

    Finally had the chance to look into this. It wasn’t enough to swap file_get_contents() with wp_remote_get() – this would give an error about the line below with json_decode() needing a string.

    By wrapping $filecontent on that line in wp_remote_retrieve_body() (thanks Google!), the code now works. Full function below.

    function full_comment_count() {  
    global $post;  
    $url = get_permalink($post->ID);  
          
    $filecontent = wp_remote_get('https://graph.facebook.com/?ids=' . $url);
    $json = json_decode(wp_remote_retrieve_body($filecontent)); 
    $count = $json->$url->comments;  
    $wpCount = get_comments_number();  
    $realCount = $count + $wpCount;  
    if ($realCount == 0 || !isset($realCount)) {  
        $realCount = 0;  
    }  
    return $realCount;  
    }
Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘PHP error after updating PHP version’ is closed to new replies.