curl发送json格式数据
php的curl方法详细的见官方手册。
curl_setopt用法: http://www.php.net/manual/en/function.curl-setopt.php
<?php $params = array(
'par1' => 'a',
'par2' => 11,
);
$header = array("Content-type: application/json");// 注意header头,格式k:v
$arrParams = json_encode($params); $ch = curl_init();
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arrParams);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);// curl函数执行的超时时间(包括连接到返回结束) 秒单位
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);// 连接上的时长 秒单位
curl_setopt($ch, CURLOPT_URL, $url);
$ret = curl_exec($ch); $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);// 对方服务器返回http code
curl_close($ch); // deal $ret
值得注意的是,json_encode()
在处理中文问题上,可以控制是否转为unicode格式,常量JSON_UNESCAPED_UNICODE这个控制。详情见http://php.net/manual/zh/function.json-encode.php
重试的curl方法:
<?php
function callCurl($url, $arrParams, $format, $timeout = 15, $retry = 3){
$ch = curl_init();
if('json' === $format){// header
$arrParams = json_encode($arrParams);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
}
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arrParams);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_URL, $url);
$ret = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
while($http_status != 200 && $retry--){
$ret = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
}
//记录请求日志
$arrLog = array(
'url' => $url,
'arrParams' => $arrParams,
'http_status' => $http_status,
'curl_error' => curl_error($ch),
'retry' => $retry,
'result' => $ret,
);
// 记录log
curl_close($ch);
$result = json_decode($ret, true);
return $result;
}
curl发送json格式数据的更多相关文章
- 利用 CURL 发送JSON格式字符串
/* * post 发送JSON 格式数据 * @param $url string URL * @param $data_string string 请求的具体内容 * @return array ...
- idHTTP 向网站发送json格式数据
idHTTP 向网站发送json格式数据 var rbody:tstringstream; begin rbody:=tstringstream.Create('{"name":& ...
- 巧妙的使用jmeter来发送json格式数据
1. header-manager 修改content-type值. 如果不修改该值, 则默认会是urlencode的数据格式(例如a=5&b=6). 修改为json后,会告诉服务器,发送的数 ...
- linux c 使用socket 发送http请求 可以发送json格式数据
#include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#include <time.h ...
- postman 发送json 格式数据
hearder 设置 body 设置 { ", "primary_content": "{\"SN\":129,\"MPBH\&q ...
- iOS-AFN Post JSON格式数据
- (void)postRequest{ AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; // >>> ...
- 如何使用python内置的request发送JSON格式的数据
使用步骤如下: 一.如果想发送json格式的数据,需要使用request模块中的Request类来创建对象,作为urlopen函数的参数 二.header中添加content-type为applica ...
- ajax发送json格式与文件数据、django自带的序列化器(了解)
上期内容回顾 聚合查询和分组查询 # 聚合查询 max min sum avg count # 查询关键字:aggregate from django.db.models import Max, Mi ...
- 使用jQuery发送POST,Ajax请求返回JSON格式数据
问题: 使用jQuery POST提交数据到PHP文件, PHP返回的json_encode后的数组数据,但jQuery接收到的数据不能解析为JSON对象,而是字符串{"code" ...
随机推荐
- Oracle 常用的查询操作
–1. 查询系统所有对象select owner, object_name, object_type, created, last_ddl_time, timestamp, statusfrom db ...
- Oracle win32_11gR2_database在Win7下的安装与卸载
Oracle win32_11gR2_database在Win7下的安装与卸载 by:授客 QQ:1033553122 Oracle的硬件要求 在安装oracle之前,请检查一下自己的电脑硬件是否复合 ...
- Java并发编程--总结
1.并发优势 2.并发安全性 3.并发优化 4.并发内存模型 https://pan.baidu.com/s/1J4S2tC7YDgdOwtAg1970sg
- MVC model验证 获取验证错误信息
public static class ModelStateExtensions { /// <summary> /// 获取model验证错误信息 /// </summary> ...
- yum-config-manager命令找不到
安装: 命令在yum-utils 包里,安装既可以解决:yum -y install yum-utils 当然你也可以通过 dnf 替代 dnf 命令替代: 禁用仓库命令就是: dnf config- ...
- 上下文管理器——with语句的实现
前言 with语句的使用给我们带来了很多的便利,最常用的可能就是关闭一个文件,释放一把锁. 既然with语句这么好用,那我也想让我自己写的代码也能够使用with语句,该怎么实现? 下面具体介绍怎样实现 ...
- cc1plus.exe: error: unrecognized command line option "-fno-keep-inline-dllexport "
在Windows环境下的控制台上,通过qmake指令编译Qt程序时,出现 cc1plus.exe: error: unrecognized command line option "-fno ...
- 第 16 章 C 预处理器和 C 库(预定义宏)
/*------------------------------------- predef.c -- 预定义宏和预定义标识符 ------------------------------------ ...
- JRE、JDK概述
JRE(java Runtime Environment java运行环境) 包括java虚拟机(JVM Java Virtual Machine)和Java程序所需的核心类库等, 如果想要运行一个开 ...
- nginx+uwsgi+djangos部署项目完整
1.基本信息 Linux 版本:SentOS7.4 Python 版本:3.7.1 2.下载 uWSGI pip3 install uwsgi uwsgi 主要的任务是座位分发路由的服务器. 先写一个 ...