How to run wp_remote_get() inside of a loop for multiple page API response?

If I’m reading the docs right, wp_remote_get() will return a WP_Error on failure, but not necessarily on a 404/400/403 (etc) status code from your remote server. (Though that will depend in part on what your remote server sends back if there’s an invalid request.) You can check the status code using wp_remote_retrieve_response_code(). I’d use something … Read more

Where would I put my call to wp_remote_get?

I’m guessing I should make a function that: Calls wp_remote_get(“https://jsonplaceholder.typicode.com/todos”) Returns the array of job posting objects Yes, that still leaves the question of where to call it in functions.php, but I’m not sure how I would export it so it would be available in the view. Functions in functions.php are available in templates, making … Read more

rewrite script to use wp_remote_get instead of file_get_contents_curl

You can try this: foreach( $posts as $post ) { $url = sprintf( ‘http://graph.facebook.com/?id=%s’, get_permalink( $post->ID ) ); $response = wp_remote_get( $url, array( ‘timeout’ => 15 ) ); if( ! is_wp_error( $response ) && isset( $response[‘response’][‘code’] ) && 200 === $response[‘response’][‘code’] ) { $body = wp_remote_retrieve_body( $response ); $fb = json_decode( $body ); if( ! … Read more

wp_remote_get(), downloading and saving files

Well here’s the solution that I came up with: // Use wp_remote_get to fetch the data $response = wp_remote_get($url); // Save the body part to a variable $zip = $data[‘body’]; // In the header info is the name of the XML or CVS file. I used preg_match to find it preg_match(“/.datafeed_([0-9]*)\../”, $response[‘headers’][‘content-disposition’], $match); // Create … Read more