PHP curl模拟post提交json数据 PHP基于curl模拟post提交json数据示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

PHP curl模拟post提交json数据 PHP基于curl模拟post提交json数据示例

爱代码也爱生活   2021-03-31 我要评论

本文实例讲述了PHP基于curl模拟post提交json数据。分享给大家供大家参考,具体如下:

这里php模拟post提交json数据操作的关键是在头部设置Content-Type

<?php
header("Content-type:application/json;charset=utf-8");
$url="http://192.168.10.234:8080/uc/login/loginid";
$param=array(
  //注册字段
  "name"=>"test001",
  "pass"=>"xxxx",
);
$data = json_encode($param);
list($return_code, $return_content) = http_post_data($url, $data);//return_code是http状态码
print_r($return_content);exit;
function http_post_data($url, $data_string) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json; charset=utf-8",
    "Content-Length: " . strlen($data_string))
  );
  ob_start();
  curl_exec($ch);
  $return_content = ob_get_contents();
  ob_end_clean();
  $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  return array($return_code, $return_content);
}

PS:这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

在线JSON代码检验、检验、美化、格式化工具:
http://tools.softyun.net/code/json

JSON在线格式化工具:
http://tools.softyun.net/code/jsonformat

在线XML/JSON互相转换工具:
http://tools.softyun.net/code/xmljson

json代码在线格式化/美化/压缩/编辑/转换工具:
http://tools.softyun.net/code/jsoncodeformat

C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.softyun.net/code/ccode_html_css_json

希望本文所述对大家PHP程序设计有所帮助。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们