0

Previously I am using the below code. Now I need to change this code and replace file_get_contents() with wp_safe_remote_get(). I need to know how to set 'header' => "Accept: application/json" in wp_safe_remote_get():

$opts = array(
        'http' => array(
            'method' => "GET",
            'header' => "Content-Type:application/json",
            'header' => "Accept: application/json"
        )
    );

$context = stream_context_create($opts);
$file =   file_get_contents( 
    'http://xxxxxxxx.xxxx/xapix/xjson/xxxxx.php',
     false, 
     $context
);

I have tried to do this but not working

$args = array( 'timeout' => 30 , 'header' => "Accept: application/json");   
$file = wp_safe_remote_get('http://xxxxxxxx.xxxx/xapix/xjson/xxxxx.php', $args); 
3
  • so what have you tried? Commented Feb 18, 2016 at 11:05
  • @MarkKaplun $args = array( 'timeout' => 30 , 'header' => "Accept: application/json" ); $file = wp_safe_remote_get('xxxxxxxx.xxxx/xapix/xjson/xxxxx.php', $args); I have tried this code but not working
    – Anudeep GI
    Commented Feb 18, 2016 at 11:17
  • so please edit your question to include that... and explain why do you think that the headers are not being send. Also it might be useful to know your server enviroment, do you have curl enabled or some other way in which http requests are being handled Commented Feb 18, 2016 at 11:21

1 Answer 1

1

This will work

$args = array( 'headers' => array("Accept" => "application/json"));
$file = wp_safe_remote_get('http://xxxxxxxx.xxxx/xapix/xjson/xxxxx.php', $args);

headers is an array. Not a string .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.