场景:在调用第三方接口时经常需要使用到curl进行数据交互,在初次使用时遇到一些小问题,记录下来随时查阅。

封装curl相关方法便于使用,方法如下:

/**
* @param $url
* @param string $error
* @param array|string $post
* @param int $timeout
* @param null $ref
* @param string $ua
* @param $contentType
* @return bool|mixed
*/
function xcurl($url, &$error = "", $post = array(), $timeout = 5, $ref = null, $ua = "Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre", $contentType) {
$ch = curl_init(); if(!empty($ref)) {
curl_setopt($ch, CURLOPT_REFERER, $ref);
} curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); if (false !== stripos($url, "https://")) { # https处理,不校验相关证书
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
} if(!empty($ua)) {
curl_setopt($ch, CURLOPT_USERAGENT, $ua);
} if(count($post) > 0){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
} if ('json' == $contentType) {
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($post)));
} $output = curl_exec($ch);
if ($output === false) {
$error = curl_error($ch);
curl_close($ch);
return false;
} else {
curl_close($ch);
return $output;
}
}

调用如下:

<?

$url = 'localhost/php/server.php';
$error = '';
$post = [
'hello' => 'world',
'lang' => 'php',
];
$post = json_encode($post); $result = xcurl($url,$error, $post, 5, null, 'Mozilla/5.0 (X11; Linux x86_64; rv:2.2a1pre) Gecko/20110324 Firefox/4.2a1pre', 'json'); var_dump($result);

本地服务接受参数时遇到了问题,无论$_POST还是$_REQUEST都无法获取curl客户端发送的json,所以改用file_get_contents来获取,代码:

print_r(file_get_contents('php://input'));

最终请求curl.php获取到结果为:

/code/php/curl.php:19:string

 '{"hello":"world","lang":"php"}' (length=30)

php中使用curl来post一段json数据的更多相关文章

  1. PHP中使用CURL实现GET和POST请求数据

    PHP中使用CURL实现GET和POST请求 一.什么是CURL? cURL 是一个利用URL语法规定来传输文件和数据的工具,支持很多协议,如HTTP.FTP.TELNET等.最爽的是,PHP也支持 ...

  2. Java之Hashmap中value为null,则返回json数据中key不存在

    前两天干活儿的时候,将实例对象放在Hashmap中返回给前端: ArtificialEntity artificialEntity = artificialService.getInfoById(id ...

  3. centos curl命令post携带body json数据

    1,接口链接 https://xxx.com/xqAppServer/api/APPBizRest/idfaDuplicateRemove/v1/?sysIdfa=661743D1-A76E-498A ...

  4. curl向web服务器发送json数据

    c++使用libcurl: /* *g++ demo.cpp -g -Wall -lcurl */ #include <string.h> #include <stdlib.h> ...

  5. php中的curl使用入门教程和常见用法实例

    摘要: [目录] php中的curl使用入门教程和常见用法实例 一.curl的优势 二.curl的简单使用步骤 三.错误处理 四.获取curl请求的具体信息 五.使用curl发送post请求 六.文件 ...

  6. Android中Json数据读取与创建

    一:  Json的特性和在数据交互中的地位就不用说了,直接看案例. 首先在android studio中创建assets文件目录,用于存放Json数据文件,android studio 1.3 默认项 ...

  7. Android中Json数据读取与创建的方法

    转自:http://www.jb51.net/article/70875.htm 首先介绍下JSON的定义,JSON是JavaScript Object Notation的缩写. 一种轻量级的数据交换 ...

  8. 我的Android进阶之旅------>解决Jackson、Gson解析Json数据时,Json数据中的Key为Java关键字时解析为null的问题

    1.问题描述 首先,需要解析的Json数据类似于下面的格式,但是包含了Java关键字abstract: { ret: 0, msg: "normal return.", news: ...

  9. php – 通过curl从url获取JSON数据

    我试图通过curl连接从URL获取JSON数据.当我打开链接时:它显示{“version”:“N / A”,“success”:true,“status”:true}.现在,我希望获得以上内容. 到目 ...

随机推荐

  1. EffectManager

    using UnityEngine; using System.Collections; public class EffectManager : MonoBehaviour { public Ani ...

  2. 【Java】Java_11运算符

    1.运算符(operator)  Java 语言支持如下运算符: 算术运算符:  +,-,*,/,%,++ 赋值运算符 = 关系运算符:  >,<,>=,<=,==,!=  i ...

  3. 转:介绍一个好用的抓取dump的工具-ProcDump

    介绍一个好用的抓取dump的工具-ProcDump Procdump是一个轻量级的Sysinternal团队开发的命令行工具, 它的主要目的是监控应用程序的CPU异常动向, 并在此异常时生成crash ...

  4. windows服务器下配置memcache最大内存值

    windows服务器下修改memcache配置要求如下: 1.用内网ip的方式提供web应用服务器调用,不允许直接通过外网调用,如将memcache服务放在192.168.1.168的服务器上 2.修 ...

  5. jQuery select的操作实现代码

          //改变时的事件  $("#testSelect").change(function(){ //事件发生生 jQuery('option:selected', this ...

  6. Linux selinux关闭方法和防火墙关闭方法

    在Linux下设置selinux有三种方法.一.在图形界面中:    桌面-->管理-->安全级别和防火墙,设置为disable.二.在命令模式下:    修改文件:/etc/selinu ...

  7. unity, 由于project settings中time scale变成0导致动画不播放

    在Mac和iOS间多次switch platform之后,忽然发现开始scene的动画不播了.进入游戏后再切回来动画恢复正常. 检查了scene的逻辑,发现没有任何问题.删除了Temp和Library ...

  8. vivado与modelsim的联合仿真(一)

    vivado软件中也自带仿真工具,但用了几天之后感觉仿真速度有点慢,至少比modelsim慢挺多的.而modelsim是我比较熟悉的一款仿真软件,固然选它作为设计功能的验证.为了将vivado和mod ...

  9. Ionic学习笔记4_ionic路由(页面切换)

    1.1.  ionic路由机制: 状态 1.2.  ion-nav-view <body ng-controller="firstCtrl"> <a class= ...

  10. 28. Implement strStr()【easy】

    28. Implement strStr()[easy] Implement strStr(). Returns the index of the first occurrence of needle ...