场景:在调用第三方接口时经常需要使用到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. Implementing DDD Reading - Strategic Design

    1. 概念篇 1.1 领域 广义上讲,领域即是一个组织所做的事情以及其中所包含的一切,也是组织的业务范围以及在其中所进行的活动.软件所讨论的领域即是这个组织的领域,应该是清晰明确的.不同的层面或粒度, ...

  2. Java Executor框架

    java.util.concurrent 包中包含灵活的线程池实现,但是更重要的是,它包含用于管理实现 Runnable 的任务的执行的整个框架,该框架称为 Executor 框架.该框架基于生产者- ...

  3. Stack Exchange 的架构

    近日,Stack Exchange系统管理员blog上发布了一篇关于Stack Exchange的架构一瞥,其包括了Stack Overflow, Server Fault 和 Super User的 ...

  4. 【原】Ubuntu下使用teamviewer

    想尝试直接使用Xmanager打开Ubuntu桌面版,结果一直存在问题,迂回一下,尝试使用teamviewer解决,结果OK.办法如下: 在Ubuntu中,下载teamviewer,通过Windows ...

  5. transition状态下Mecanim动画的跳转

    来自: http://blog.csdn.net/o_oxo_o/article/details/21325901 Unity中Mecanim里面动画状态的变化,是通过设置参数(Parameter)或 ...

  6. JavaScriptl 类数组转换为数组

    slice和Array.form方法,具体见示例代码: <!DOCTYPE html> <html lang="zh"> <head> < ...

  7. openstack架构简单介绍J版(更新中)

    title : OPENSTACK架构简单介绍 openstack的发展及历史 openstack是什么? OpenStack是一个美国国家航空航天局和Rackspace合作研发的云端运算‎软件,以A ...

  8. android adt 最新下载地址23.03

    打开这个网址就可以看到adt的详细信息:  http://developer.android.com/sdk/installing/installing-adt.html 或者直接在你的eclipse ...

  9. grep和map计算两个集合交集、并集、补集

    #!/usr/bin/perl use strict; ######################################## 用grep 和map 获取两个列表的交集并集.补集###### ...

  10. json2.js

    /* http://www.JSON.org/json2.js 2010-03-20 Public Domain. NO WARRANTY EXPRESSED OR IMPLIED. USE AT Y ...