How to set debug to true
If you are having trouble getting your API to output data, the first step should be setting the ‘debug’ parameter to ‘true’. This will output a whole bunch of debugging information which can assist in finding the issue.
Using the Binance API as an example, we can see how to set debug to true on both the template tag and the shortcode.
Template tag
echo wpgetapi_endpoint( 'binance', 'price_of_coin', array('debug'='true') );
Shortcode
[wpgetapi_endpoint api_id='binance' endpoint_id='price_of_coin' debug='true']
Follow these links for help using the shortcode and help using the template tag.
A valid URL was not provided error
If you get the error shown below, it is usually because you have a space within your API ID or within your Endpoint ID in the settings of WPGetAPI.
These ID’s need to be lowercase with no spaces like this: “this_is_my_id” rather than “This Is My ID”
{"errors":{"http_request_failed":["A valid URL was not provided."]},"error_data":[]}
The template tag returns a Warning or says ‘Array’
If you are using the template tag and it outputs either of the following:
- Array
- Warning: Array to string conversion….
The likely cause is that you have tried to ‘echo’ the data when the Results Format is set to PHP array data, as shown below.
// wrong way to do it when set to PHP array data
echo wpgetapi_endpoint( 'binance', 'price_of_coin', array('debug'='false') );
// correct way to do it when set to PHP array data
// you can then use the $data variable however you like
$data = wpgetapi_endpoint( 'binance', 'price_of_coin', array('debug'='false') );
// wpgetapi_pp is a helper function for 'pretty printing' the raw data
wpgetapi_pp($data);
The other option is to simply set the Results Format to JSON string and echo the data but this may not return the desired results as it would return a JSON formatted string.
415 Unsupported Media Type error
This response code error indicates that the server refuses to accept the request because the payload format is in an unsupported format. This can usually be rectified by setting the ‘Content-Type’ to ‘application/json’ in the headers as shown below.
cURL error 28: Operation timed out after 10001 milliseconds
The cURL error 28 indicates that the server (where the API is hosted) is not responding within the default time of 10 seconds. This could be because the API server may be down or not responding, or the data that is being returned takes longer than 10 seconds to come back to you.
The default time can be increased by adding this snippet. The below snippet changes the timeout value to 30 seconds, but you can set this to whatever you like.
function wpgetapi_set_request_args_parameters( $args ) {
$args['timeout'] = 30;
return $args;
}
add_filter( 'wpgetapi_default_request_args_parameters', 'wpgetapi_set_request_args_parameters' );
If this error persists, there are some additional steps you can try. You can find more information here: https://www.wpbeginner.com/wp-tutorials/how-to-fix-curl-error-28-connection-timed-out-after-x-milliseconds/