0

i want to connect to API.but i need to send data with post, but receive in json.

    <?php
$params = array (
'user_name' => '1521',
'user_password' => '8554',
'webservice_user'=>'test',
'token'=>'202cb962ac59075b964b07152d234b70');
$query = http_build_query ($params);
$contextData = array (
'method' => 'POST',
'header' => "Connection: close\r\n".
"Content-Length: ".strlen($query)."\r\n",
'content'=> $query );
$context = stream_context_create (array ( 'http' => $contextData ));
// Read page rendered as result of your POST request
$json = file_get_contents('url_here');
$obj = json_decode($json);
echo $obj->access_token;
$result =  file_get_contents (
'IP_ex/login',
false,
$context);
echo($result);
?>

when i run my code, receive that Error:

file_get_contents(url_here): failed to open stream

user2254798
  • 553
  • 2
  • 6
  • 17
  • Possible duplicate of [Send json post using php](http://stackoverflow.com/questions/6213509/send-json-post-using-php) – bashoogzaad Jan 16 '17 at 09:47

1 Answers1

1

To receive in in json format, you should do echo json_encode($myArray) as

<?PHP
header('Content-Type: application/json');
echo json_encode($myArray);

if you want to send a post request to a server, you should use cURL or something similar.

samayo
  • 16,163
  • 12
  • 91
  • 106
  • I will be grateful to give me very simple example – user2254798 Jan 16 '17 at 09:45
  • This one is simple http://stackoverflow.com/a/29601842/1640606 but you have to download the Guzzle library. or this one for curl http://stackoverflow.com/a/2138534/1640606 – samayo Jan 16 '17 at 09:50