0

I'm trying to get some json data from another server that needs 3 parameters so that I can access it. I saw this code in internet but its not working. I have searched a lot and I haven't found a solution. When I run this code I get :

use pass and code are missing

(is the response from the server I'm requesting), I hope I made things clear if there's anything I didn't explain please do tell.

    $url = 'http://xxxx/.js';
    $data =http_build_query(array('user' => 'xx',
            'pass' =>'xxx ',
               'code' =>'xxx')
  );


    $options = array(
   'http' => array(
    'header'  => "Content-type: application/json\r\n",
    'method'  => 'POST',
    'content' => $data
    )  
    );
     $context= stream_context_create($options);
     $result = file_get_contents($url, false, $context);
      if ($result === FALSE) { }

     echo $result ;
James Jones
  • 3,632
  • 5
  • 21
  • 41
  • https://stackoverflow.com/questions/2445276/how-to-post-data-in-php-using-file-get-contents Replace header definitions. – eustatos Aug 03 '18 at 12:53
  • @eustatos good anser for `x-www-form-urlencoded` but not JSON – Scuzzy Aug 03 '18 at 12:59
  • I edited your question to add ........, ''''''''' and capital letters. I recommend using them next time, it makes your question easier to read. – James Jones Aug 03 '18 at 13:46

1 Answers1

1

Try to change your payload to json...

$data = json_encode( array(
  'user' => 'xxx',
  'pass' => 'xxx',
  'code' => 'xxx'
) );

and to remove \r\n from your content type

"Content-type: application/json"
Scuzzy
  • 11,272
  • 1
  • 40
  • 42