我想连接到API。但我需要发送带有post的数据,但是在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);
?>
当我运行我的代码时,收到错误:
file_get_contents(url_here):无法打开流
答案 0 :(得分:1)
要以json格式接收,您应该echo json_encode($myArray)
作为
<?PHP
header('Content-Type: application/json');
echo json_encode($myArray);
如果您要向服务器发送帖子请求,则应使用cURL或类似内容。