Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress How to Build a WordPress Plugin Connecting WordPress Plugins with 3rd Party APIs Getting and Storing a JSON Feed

Umeed Emad
PLUS
Umeed Emad
Courses Plus Student 1,150 Points

Can not connect to teamhouse url

Hello,

When I try to var_dump the user profile I get this error:

object(WP_Error)[308] private 'errors' => array (size=1) 'http_request_failed' => array (size=1) 0 => string 'Failed to connect to teamtreehouse.com port 80: Network unreachable' (length=67) private 'error_data' => array (size=0) empty

11 Answers

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Can you post the code you're using to get the object. It shouldn't be throwing an error.

Umeed Emad
PLUS
Umeed Emad
Courses Plus Student 1,150 Points

Hi Zac thanks for the reply here is the code for getting json feed url:

function webscriber_profile_get_username($webscriber_username){
    $json_feed_url = 'http://teamtreehouse.com/' . $webscriber_username . '.json';
    $args = array( 'timeout' => 500 );

    $json_feed = wp_remote_get ( $json_feed_url, $args );

    $webscriber_profile = json_decode ( $json_feed['body'] );

    return $webscriber_profile;
}
Umeed Emad
PLUS
Umeed Emad
Courses Plus Student 1,150 Points

Hey Zac did you get my last message with sample code? I cant remove this error please help so I can continue the course.

Thanks

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

If you var dump $json_feed right after you set it, what does it output. Comment out the last two lines for the moment.

Umeed Emad
PLUS
Umeed Emad
Courses Plus Student 1,150 Points

Still error

object(WP_Error)[307]
  private 'errors' => 
    array (size=1)
      'http_request_failed' => 
        array (size=1)
          0 => string 'Failed to connect to teamtreehouse.com port 80: Network unreachable' (length=67)
  private 'error_data' => 
    array (size=0)
      empty
Umeed Emad
PLUS
Umeed Emad
Courses Plus Student 1,150 Points

And here is the modified code

function webscriber_profile_get_username($webscriber_username){

    $json_feed_url = 'http://teamtreehouse.com/' . $webscriber_username . '.json';
    $args = array( 'timeout' => 500 );

    $json_feed = wp_remote_get ( $json_feed_url, $args );

    var_dump ( $json_feed );

    /*$webscriber_profile = json_decode ( $json_feed['body'] );

    return $webscriber_profile;*/

}
Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Okay. What about if you echo out $json_feed_url and paste it in your browser, what do you get? Also try setting timeout to double if that helps.

Umeed Emad
PLUS
Umeed Emad
Courses Plus Student 1,150 Points

When I echo the $json_feed_url in my browser I get the correct link for the json file. http://teamtreehouse.com/umeedemad.json

but setting timeout to double does not change anything same error. I am running the code in my localhost. is there any specific setting I need to do with php.ini for connection?

Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

Can you try throwing this up on a server to see if it works there? That will at least tell us if it's a local access issue.

Nicholas Pretorius
Nicholas Pretorius
18,683 Points

This thread is from quite some time ago, but I had a similar problem. Try the following:

First: Make sure that your php.ini file has this setting:

allow_url_include = On

See: http://wordpress.stackexchange.com/questions/185816/http-request-on-localhost-failing

If you aren't working behind a proxy, wp_remote_get() should work. If you are working behind a proxy, see below:

Second: When working behind a proxy, you need to setup your wp-config.php according to the following: http://wordpress.stackexchange.com/questions/75048/how-to-use-the-http-api-with-a-proxy

Hope this helps!

Jonathan Perez
Jonathan Perez
9,654 Points

I'm having the same problems but nothing I've tried works - I suspect this has to do with SSL. I'm using MAMP and it's been close to impossible to make it work with SSL (https://) so I haven't been able to go past this video :(

BTW: the error I get is "cURL error 35: Unknown SSL protocol error in connection to teamtreehouse.com:443"

The verbose error is:

object(WP_Error)[833] public 'errors' => array (size=1) 'http_request_failed' => array (size=1) 0 => string 'cURL error 35: Unknown SSL protocol error in connection to teamtreehouse.com:443 ' (length=81) public 'error_data' => array (size=0) empty