簡體   English   中英

如何用POST發送數據並用php中的json接收

[英]how to send data with POST and receive with json in php

我想連接到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):無法打開流

要以json格式接收,您應該將echo json_encode($myArray)做為echo json_encode($myArray)

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

如果要向服務器發送發布請求,則應使用cURL或類似的名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM