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 ;
3
  • stackoverflow.com/questions/2445276/… Replace header definitions. Commented Aug 3, 2018 at 12:53
  • @eustatos good anser for x-www-form-urlencoded but not JSON Commented Aug 3, 2018 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. Commented Aug 3, 2018 at 13:46

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

2 Comments

If this doesn't work you may need to use CURL.... stackoverflow.com/questions/16920291/…
Try removing the \r\n from your content type too.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.