Using cURL to Test the WordPress Theme and Plugin APIs
Occasionally (rarely) when trying to search for a theme or plugin via the WordPress dashboard, you may see an error like this:
An unexpected error occurred. Something may be wrong with WordPress.org or this server’s configuration. If you continue to have problems, please try the support forums.
This means that the request WordPress has made to the theme or plugins api has failed, or that the body of the response is bad or empty. Often web hosts will turn off outbound http requests and this will be the source of your problem. However, there can be a myriad of other issues that may cause this error. WordPress will use one of three “transports” and search for them on your server in this order: curl, streams, and fsockopen. Since the focus of this article is on using cURL, that’s what will use at the command line.
To check if cURL is installed on your server, use the Unix ‘which’ command to find it’s install location.
and you should get a response something like this (your path may vary):
To simply check connectivity with the WordPress theme and plugin apis, you can make an http HEAD request with cURL:
$ curl -I http://api.wordpress.org/plugins/info/1.0/
$ curl -I http://api.wordpress.org/themes/info/1.0/
You should see output something like this:
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 09 Jul 2018 16:41:02 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
If you don’t have connectivity, you may see something like this:
>curl: (6) Could not resolve host: api.wordpress.org; nodename nor servname provided, or not known
If you want to duplicate the request made when you’re actually on the WordPress dashboard, you’ll have to make a POST request with serialized data parameters. To mimic a search for a “blue” theme, use this cURL command:
$ curl --data 'action=query_themes&request=O:8:"stdClass":4:{s:4:"page";i:1;s:8:"per_page";i:36;s:6:"fields";N;s:6:"search";s:4:"blue";}' http://api.wordpress.org/themes/info/1.0/
To mimic a search for a “cache” plugin, use this command:
$ curl --data 'action=query_plugins&request=O:8:"stdClass":3:{s:4:"page";i:1;s:8:"per_page";i:30;s:6:"search";s:5:"cache";}' http://api.wordpress.org/plugins/info/1.0/
A successful request will return quite a bit of HTML and serialized data (which I won’t post here).