json - using wp_remote_get instead of file_get_contents

This question already has answers here:rewrite script to use wp_remote_get instead of file_get_contents_curl(4 answers)C

This question already has answers here: rewrite script to use wp_remote_get instead of file_get_contents_curl (4 answers) Closed 11 years ago.

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.

This question already has answers here: rewrite script to use wp_remote_get instead of file_get_contents_curl (4 answers) Closed 11 years ago.

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.

Share Improve this question edited Jan 14, 2020 at 11:44 J. Scott Elblein 1056 bronze badges asked Sep 27, 2013 at 9:35 DavidDavid 1412 gold badges3 silver badges9 bronze badges 5
  • 2 This question/answer can help you – cybmeta Commented Sep 27, 2013 at 9:45
  • hi, thanks for that, did help, so assumed it was saying wp-Remote did all the decoding for me already? So did $url = 'url' . $var; $output = wp_remote_get( $url ); but did not work – David Commented Sep 27, 2013 at 10:01
  • You have to read again the link I posted. If you make $output = wp_remote_get( $url ); the $output will be an array where you can access to $output['headers'], $output['body']. I think what you want is $json = json_decode($output['body']); – cybmeta Commented Sep 27, 2013 at 10:05
  • Don't use json_decode($output['body'] instead use wp_remote_retrieve_body( $output ) – Horttcore Commented Sep 27, 2013 at 10:10
  • Despite being closed as a duplicate this question is a lot better written and deals with the single issue of how to start using wp_remote_get in place of file_get_contents or file_get_contents_curl - and because the question is clearer, the answers are better and easier to understand. – squarecandy Commented Aug 26, 2020 at 18:27
Add a comment  | 

3 Answers 3

Reset to default 11

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:

$request  = wp_remote_get( 'http://example' );
$response = wp_remote_retrieve_body( $request );
if ( 
    'OK' !== wp_remote_retrieve_response_message( $response )
    OR 200 !== wp_remote_retrieve_response_code( $response )
)
    wp_send_json_error( $response );

wp_send_json_success( $response );

Both wp_send_json_success/_error() functions are wrappers for wp_send_json(), which includes wp_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

<?php
$request = wp_remote_get('http://example');
$response = wp_remote_retrieve_body( $request );
echo $response;
?>

Check the documentation for possible arguements

You can use wp_remote_get() in the following way:

$url = 'url' . $var;
$request =   wp_remote_get($url);
// Get the body of the response
$response = wp_remote_retrieve_body( $request );
// Decode the json
$output = json_decode( $response ); 

$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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744837524a4596355.html

相关推荐

  • json - using wp_remote_get instead of file_get_contents

    This question already has answers here:rewrite script to use wp_remote_get instead of file_get_contents_curl(4 answers)C

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信