[转载]php curl post raw json参数

php curl post raw json参数


     
     
     
     
  1. $params = [
  2. "title"=> "sdf",
  3. "location"=> "sdf",
  4. "imgs"=>[
  5. "sdfsd",
  6. "sdfsd"
  7. ]
  8. ];
  9. $ch = curl_init();
  10. curl_setopt($ch, CURLOPT_URL, "http://test.com/dump.php" );
  11. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
  12. curl_setopt($ch, CURLOPT_POST, 1 );
  13. curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($params)); // 必须为字符串
  14. curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: text/plain')); // 必须声明请求头
  15. $return = curl_exec($ch);
  16. var_dump($return);
  17. ?>

主要两个地方:

curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($params));
curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Content-Type: text/plain'));



接收参数dump.php


     
     
     
     
  1. print_r(file_get_contents( 'php://input'));
  2. ?>

你可能感兴趣的:(php)