[wp-hackers] help understanding wp_remote_get function and fatal WP_Error

Dion Hulse (dd32) wordpress at dd32.id.au
Tue Feb 26 02:30:05 UTC 2013


Firstly, you should have some kind of caching on that, wp_remote_get()
will not cache the data and will fetch it every time you call it.

Next, you should check the return from the function.
It can return a WP_Error instance if an error occurs, you can check
for that with the is_wp_error() function, so:

function myfunkyfunction() {
        $resp = wp_remote_get(
'http://mydomain.com/dir1/dir2/dir3/my-text-file.txt' );
        if ( ! is_wp_error( $resp ) && 200 == $resp['response']['code'] ) {
                $body = $resp['body'];
                }
        else {
                $body = "Oops - sorry there was a problem loading the stats";
        }
        return $body;
}
add_shortcode('woi102stats','myfunkyfunction');


On 26 February 2013 13:14, Paul Menard <paul at codehooligans.com> wrote:
> Try dumping the $resp after the call to wp_remote_get()
>
> Add this line before the IF
>
> echo "resp<pre>". print_r($resp, true) ."</pre>";
>
> See what the full WP_Error reads. You should always check the return value before just processing.
>
> My thoughts anyway.
>
> P-
>
>
> On Feb 25, 2013, at 9:07 PM, The Avanti Group <chuck at avantigroup.com> wrote:
>
>> Hi WP-Hackers ... i'm hoping someone on this list might be able to help shed light on why my wp_remote_get function is generating fatal errors ...
>>
>> the php error log says "Cannot use object of type WP_Error as array in /mysite/wp-content/themes/mychildtheme/functions.php on line 288" ...
>>
>> said line 288 is this one -> if ( 200 == $resp['response']['code'] ) {
>>
>> and my entire function is listed below ... this function is calling a text file thus not sure if that is the problem in that wp_remote_get only works with html files (e.g., head, title, and body tags) ..??.. or if i am missing something more basic (e.g., like a comma between ['response'] ['code'] or what ..??..
>>
>> accordingly, thanks in advance for any pointers that can be shared ... cordially, chuck scott
>>
>>
>> ===============
>> here is my function that iis responsible for error ->
>> ===============
>> function myfunkyfunction() {
>>        $resp = wp_remote_get( 'http://mydomain.com/dir1/dir2/dir3/my-text-file.txt' );
>>        if ( 200 == $resp['response']['code'] ) {
>>                $body = $resp['body'];
>>                }
>>        else {
>>                $body = "Oops - sorry there was a problem loading the stats";
>>        }
>>        return $body;
>> }
>> add_shortcode('woi102stats','myfunkyfunction');
>>
>>
>> ps - i'm using latest version of WP 3.5.1 and this function is on a sub site on a multisite network - not sure that matters but mention just in case ..??..
>>
>> - fin -
>>
>>
>>
>> ------------------------------------------------------------------------------
>> Chuck Scott -> Information Architect
>> ------------------------------------------------------------------------------
>> Specialty -> WordPress Multisite Information Design
>> ------------------------------------------------------------------------------
>> Site -> http://ChuckingIt.com - Reflecting on Life,
>> Business, Entrepreneurship, Innovation & Technology
>> ------------------------------------------------------------------------------
>> What's New -> Tweeting a bit at @avantigroup
>> ------------------------------------------------------------------------------
>> ------------------------------------------------------------------------------
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list