have run my theme through the Theme checker plugin and it seems upset about use of file_get_contents
to get a json url. I’ve found posts saying i should use wp_remote_get
. I’m currently decoding the url with the following:
$url = 'url' . $var;
$json = file_get_contents($url,0,null,null);
$output = json_decode($json,true);
The message I get from the Theme Checker is:
WARNING: file_get_contents was found in the file.php possible file operations.
Is it just saying this because there is a function I could possibly use by wordpress or any other reasons? Also how would I use wp_remote_get
. I tried a few variations, mostly replacing file_get_contents with wp_remote_get
with no luck. Doesn’t seem to decode the url at all.
If you need to send a JSON response, then there’s a set of functions for that. In case you need that for an AJAX callback:
wp_remote_retrieve_response_message()
wp_remote_retrieve_response_code()
wp_send_json_success()
wp_send_json_error()
wp_send_json()
Would finally be something like that:
Both
wp_send_json_success/_error()
functions are wrappers forwp_send_json()
, which includeswp_die()
at the end. So there’s nothing else to do.Keep in mind that 99% of all remote APIs are sending
200/OK
in case of errors. You’ll still have to manually inspect the result and check for errors.Use wp_remote_get() in conjunction with wp_remote_retrieve_body()
Example
Check the documentation for possible arguements
You can use
wp_remote_get()
in the following way:$output
now has what you want and now you can go ahead and do your stuff.There is a also a series of tutorials on
wp_remote_get()
. Go through it, it will definitely help.Link — Tutorial
Hope it helps.