wp_remote_get keeps timing out

I'm making a plugin that compares data from external API with meta items in WordPress backoffice. I tried using wp_remote_get method to query my API but it doesn't return anything, nobody, nothing. When accessed directly with the same URL in browser the API generates JSON array without problems. What am I doing wrong? This is (partially omitted code in the plugin) .......... $chopped = explode("@", $meta['Email'][0]); $url = 'http://example.com/api/users/'.$chopped[0].'/'.$chopped[1]; global $wp_version; $args = array( 'timeout' => 5, 'redirection' => 5, …
Category: Web

Getting featured image with PHP and not javascript from wordpress api _embed

Not having access to the functions file, I can't add the function to add the featured image to the API so I need to use wp-json/wp/v2/posts?_embed. With javascript I believe it is ._embedded['wp:featuredmedia']['0'].source_url so I tried with php after using wp_remote_get, $post->_embedded['wp:featuredmedia'][0]->source_url but the error I am getting is : Cannot use object of type stdClass as array
Category: Web

Proxy External API request in PHP from Edit.js in Block Plugin

I'm working with a third-party API that has optional filters on their API. I'm creating a block where the user has two select elements in Edit.js that are displayed on the backend of the block. They can select filters, specialty, and location, these values are then stored in attributes. Once a user changes a select, I want to fire off a PHP request using these attributes passed to the PHP function that uses wp_remote_get() to build the proxy and dump …
Category: Web

Is there any background process that I can run from plugin without depending on page hits on a website without affecting page-load speed?

I am developing a WordPress plugin and wants to run a background task that calls an API and updates database table. Now, the API can only give results for 5 DB entries in one go and for 500 entries in my table, I have to make 100 API call. The API has allowed TPS quota of 1 and also in every 40 minutes, its old response expires which means I need to update my table if any entry is older …
Category: Web

How to convert this cURL to wp_remote_*?

My function below includes a cURL call which throws a text string at a Google Cloud text analysis API, returning a response object, which we then parse to get a specific piece. It has been recommended to me that I switch the cURL PHP statement to use WordPress' wp_remote_get. I have read the docs for that but, to be honest, I don't understand how my cURL header fields should map to wp_remote_get arguments. Or even if it should be wp_remote_post …
Category: Web

Daily automatic update of stock quotes via REST API and Cronjob. Or is there a more sophisticated way?

I'm building a stock database with WordPress. There is a custom Post Type stocks with the fields last_price and ytd_return (ACF). The database will hold around 2000 stocks. For the stock quotes I plan to use alphavantage (https://www.alphavantage.co) which offers a REST API to pull updated quotes. As I only want end-of-day data I plan the following: Every night run a "CronJob" inside Wordpress which pulls the updated stock quotes for all the stocks and calculates YTD performance. Now the …
Category: Web

Simulating timeout is not working

I am trying to simulate a timeout response when using wp_remote_get() for better debugging. This is the code I'm using: set_time_limit( -0 ); ini_set('max_execution_time', 0); $args = array( 'headers' => array( 'Content-Type' => 'application/json', 'timeout' => 15, ), ); $request_response = wp_remote_get( $this->token_url . '&sleep=10', $args ); This is the response (WP_Error): cURL error 28: Operation timed out after 5000 milliseconds with 0 bytes received It doesn't matter if I change the value of set_time_limit It doesn't matter if I …
Category: Web

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

I am using wp_remote_get() to grab some JSON from an API call. Everything is working great except that the API I am using has a limit of 100 results per response and there are just over 300 results that I need to loop over. You can get additional pages by adding &page=2, &page=3, etc. to the API URL. How would I go about refactoring this code in order to check each page of results and append the results of each …
Category: Web

Managing and deleting transients with dynamically generated transient names

Let's say I have access to a remote API that stores car data, and I want to be able to place car information box(es) into posts using a shortcode. The shortcode fetches JSON data with wp_remote_get() and server-side renders the HTML using the fetched data. It seems using the transients API would make this performant, as performing multiple API calls on page load would be slow. Because the data changes infrequently, the expiration time would be set to a day. …
Category: Web

WP_Insert_Post creating duplicate posts when logged in

I'm creating posts to a custom post type with data in a json URL. I am using the wp_ajax_nopriv action and noticed that while logged in and running the action I get duplicate posts. If I use another browser and stay logged out I get the correct number of posts. I can't figure out what in my code is causing the posts to duplicate. My function.php page: add_action('wp_ajax_nopriv_get_breweries_from_api', 'get_breweries_from_api'); add_action('wp_ajax_get_breweries_from_api', 'get_breweries_from_api'); function get_breweries_from_api() { $ogURL = 'https://api.bridgedataoutput.com/api/v2/OData/actris/Property/replication?access_token=HIDDEN&$select=Latitude,Longitude,StreetNumber,StreetName,City,StandardStatus,StateOrProvince,PostalCode,Media,ListPrice,ListingId&$unselect=ListingKey,Media.MimeType,Media.ResourceRecordKey,Media.ResourceName,Media.ClassName,Media.MediaCategory,Media.MediaObjectID,Media.MediaKey,Media.Order,Media.ShortDescription&$filter=contains(ListingId,%27888%27)'; $current_page = ( …
Category: Web

How to access specific Element in Nested JSON Array in PHP?

I have a very large JSON file output from an API which has so many nested child elements, I have been trying to figure out a way to access only specific ones but it has been a total chaos and I finally gave up and came here looking for help. here's what API return looks like: { "response": { "errors" : [], "messages" : [], "results": { "candidates" : [ { "count":{ "from": 0, "to": 39, "total": 77, }, "officials": …
Category: Web

Add additional headers to retrieve Public data from an Instagram account using wp_remote_get()

I tried to retrieve JSON data from a public instagram account using: wp_remote_get (). But the problem I have is that with the url: $url = https://www.instagram.com/USER/?__a=1 . If, I am connected to my Instagram account, the url returns the data correctly by entering the url in my browser. But, trying to send a request with the wp_remote_get ( $url) function , the response to the request returns the Login page - Instagram . do i need to add some …
Category: Web

Problem getting wp posts in plugin with wp_remote_get

I am creating a plugin to send posts to an external program. But I get a problem by getting the posts. I created the code below to get the posts and echo them. But if I run it, it goes to the empty error message. When I echo the response. It only gets 'Array' If I just post http://localhost/wordpress/wp-json/wp/v2/posts in a browser, I get a JSON with my posts What am I doing wrong? $remoteargs = array( 'timeout' => 20, …
Category: Web

How to consume and display external data in WordPress from another website

I am trying to access data from another website to display on a WordPress Website I am developing. So far I have the following: <?php /* Template Name: Testing remote data */ get_header(); <div class="main"> <div class="col-sm-12"> <header> <h2>Testing remote data</h2> </header> </div> <div class="container"> <div id="content" role="main"> <div class="col-sm-12"> <?php $url = 'http://www.bbc.co.uk/news/';// this url is only for example purposes $request = wp_remote_get( $url ); if(is_wp_error($request)) { return false; } else { $body = $request['body']; } echo $body; </div> …
Category: Web

Passing cookies when using wp_remote_get

I'm working on a plugin that fetches remote RSS feeds using wp_remote_get. The problem is that Tumblr requires a cookie for GDPR compliance. This includes RSS feeds. Which means all Tumblr feeds are currently broken. I found an example of using CURL to acquire the cookies so the feed will load. However, I have little more than a passing acquaintance with wp_remote_get and related functions. How do I implement the cookie getting hack from the example via WordPress' remote get …
Category: Web

wp_remote_get - cURL error 28 - only on same domain

On my website I use wp_remote_get to fetch data from mydomain.com/wp-json/wp/v2/pages/75 When I run it on my localhost it works fine, but after sending to production server (mydomain.com) suddenly I’m getting error: ‘cURL error 28: Operation timed out after 5000 milliseconds with 0 bytes received’ + VPS starts acting crazy (100% cpu usage and memory). It can’t retrieve data from the same domain. When I changed url to some random REST mockup it is working again. How can I get …
Category: Web

How to get the generated query string of wp_remote_get?

I have some very basic data that I am posting to a proprietary lead capturing system. Whenever I submit my form data to their system the body of the request is an error 500 page. I am trying to debug the problem with their developer, it clearly doesn't like something in my query string, and he would like to be able to test it on his end. However, I've scoured through Google results and the WordPress codex and I cannot …
Category: Web

WP_Remote_Get Not working

I have a plugin which uses wp_remote_get() and it's not working on my nginx server so I decided to test this. I created a file called test.php and inserted: <?php $response = wp_remote_get( 'http://www.domain.com/mytest.php' ); print $response ['body']; ?> When I run this file I am getting error: 2017/02/04 16:22:31 [error] 16573#16573: *461100 FastCGI sent in stderr: … "PHP message: PHP Fatal error: Uncaught Error: Call to undefined function wp_remote_get() in /var/www/html/wp-content/themes/x-child/test.php:1 I cannot tell why this would be undefined …
Category: Web

Use wp_remote_get() with a private or password protected page?

Is there any way to use the wp_remote_get() function to access a WordPress page that is either set to Private -OR- password protected (using the native password option in the post visibility settings)? The page I need to access is on the same site. I am using this function in a cron event to access this page, but I don't want this page to be accessible by the public or search bots. Googlebot is ignoring my "noindex" on the page. …
Category: Web

Use wp_remote_get to get JSON instagram feed from public profile

I'm using the curl php library to get the instagram json feed from a given public profile. I want to use wp_remote_get() because on the host I'm using for this project, I don't have the ability to use curl to request the json feed, I've noticed that my actual plugin script will work well on localhost, on netsons and aruba but not on tophost. I don't know if the function included in wordpress will do the same thing so my …
Category: Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.