加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

实例讲述了PHP基于curl模拟post提交json数据

发布时间:2023-09-21 10:09:05 所属栏目:PHP教程 来源:
导读:本文实例讲述了PHP基于curl模拟post提交json数据。分享给大家供大家参考,具体如下:

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

<?php
header("Content-type:application/json;chars

本文实例讲述了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)) 
  ); //Cuoxin.com 
  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); 

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章