鍍金池/ 問答/PHP  Linux  數(shù)據(jù)庫/ php 使用 curl ,對方也是 php 要如何接?

php 使用 curl ,對方也是 php 要如何接?

我自己寫了一個api
假設(shè)叫做sql.php

我在另一頁 sql-test.php

$data = array(
  "errno" => '1',
  "errstr" => '2',
  "errfile" => '3',
  "errline" => '4'
);
$data_string = json_encode($data);
$data = httpRequest($api_sql, $data_string);

httpRequest 是

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

我是丟json過去sql.php,而且是POST
但是我的sql.php要怎麼寫才能接到?

sql.php有一段是

(addtime, errno, errstr, errfile, errline)
    VALUES
    ('".time()."', '".$_POST['errno']."', '".$_POST['errstr']."', '".$_POST['errfile']."', '".$_POST['errline']."')

結(jié)果資料庫只有收到時間 time()
其他四個欄位的值都沒有收到 (1234)
sql.php這邊要怎麼寫才對?才能收到來自 sql-test.php 的值?

回答
編輯回答
裸橙

需要取php://input中的值
或者 你傳的時候地接傳數(shù)組 去掉json_encode這一步
接收就正常$_POST取

2018年4月10日 15:22