记一次yii下curl排错经验:

要求需要通过header传送token,数据传入方式为application/json

1.postman方式调取,没有问题,参数已json形式传过去-{}

2.原生PHP调取

        $url = Yii::$app->params['get_auto_test_detail_list'];
$params['start'] = 0;
$params['count'] = 10;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); //不是json_encode(http_build_query($params))
$a = "Bearer eyJ0eXAiOi";
        curl_setopt($ch,CURLOPT_HTTPHEADER, array("Authorization: " .$a, "Content-Type:application/json")); //数组形式,参数名字是CURLOPT_HTTPHEADER
        $output = curl_exec($ch);
        if($output === FALSE ){
echo "CURL Error:".curl_error($ch);
}
curl_close($ch);
var_dump($output);die;

3.Yii 通过Curl类 _simple_call()方法调取,

public function getAutoTestDetailList()
{
$url = Yii::$app->params['get_auto_test_detail_list'];
$params['start'] = ;
$params['count'] = ;
$curl = new Curl();
$options['HTTPHEADER'] = array("Authorization:" .self::TOKEN, "Content-Type:" .self::CONTENT_TYPE); //封装方法里面对httpheader有拼接,调用set_opt_array()
$result = $curl->_simple_call("post", $url, json_encode($params), $options); //参数json形式
if ($result !== false) {
$result = json_decode($result, true);
}
echo "<pre>";var_dump($result);echo "</pre>";
}

1.get方式传值

 function  testGet(){
$ch = curl_init (); //初始化一个cURL会话
$url = "127.0.0.1/testPage?test=test";
4
curl_setopt ( $ch, CURLOPT_URL, $url ); //设置一个cURL传输选项,url链接
curl_setopt ( $ch, CURLOPT_HEADER, 0 ); //是否传头信息
7
8 curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 ); //是否流,获取数据返回,不设置默认将抓取数据的结果返回,成功返回bool(true),失败返回bool(false)
$content = curl_exec ( $ch );           //关闭链接
var_dump($content); } function testPage(){ echo $_GET['test'];
}

2.post方式传值

function  testPost(){ 

        $ch = curl_init ();
$data["test"] = "test";
$url = "127.0.0.1/test";
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POST, 1 ); //如果你想PHP去做一个正规的HTTP POST,设置这个选项为一个非零值。这个POST是普通的 application/x-www-from-urlencoded 类型,多数被HTML表单使用
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data ); //post方式传数据
$content = curl_exec ( $ch );
curl_close ( $ch ); var_dump($content);
} function testPage(){ echo $_POST['test'];
}

3.关于php curl 编码方式 的content-type  是  multipart/form-data 还是 application/x-www-form-urlencoded

传递一个数组到CURLOPT_POSTFIELDS,CURL会把数据编码成 multipart/form-data,而传递一个URL-encoded字符串时,数据会被编码成 application/x-www-form-urlencoded"。

http://www.phperz.com/article/14/1031/32420.html  这个链接说了下两种编码的区别。而如果将post方式传输的数据 惊醒http_build_query($data) 后 ,再传输就是

application/x-www-form-urlencoded 格式 。

 

4.一个较为ok的例子

 class RemoteToolService {

     static public $timeout = 10;

     /**
* send request
*
* @param $url
* @param $type
* @param $args
* @param $charset
*
* @Returns
*/
public function send($url, $type, $args, $charset = 'utf-8') {
if ($type == 'post') {
$returnValue = $this->_post($url, $args, $charset);
} else {
$url .= '?' . http_build_query($args);
$returnValue = $this->_get($url, $charset);
}
return $returnValue;
} private function _post($url, $arguments, $charset = 'utf-8') {
if (is_array($arguments)) {
$postData = http_build_query($arguments);
} else {
$postData = $arguments;
} $ch = curl_init();
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout); $returnValue = curl_exec($ch);
curl_close($ch);
if ($charset != 'utf-8') {
$returnValue = iconv($charset, 'utf-8', $returnValue);
}
return $returnValue;
} private function _get($url, $charset = 'utf-8') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$returnValue = curl_exec($ch);
curl_close($ch);
if ($charset != 'utf-8') {
$returnValue = iconv($charset, 'utf-8', $returnValue);
}
return $returnValue;
} public function sendJson($url, $arguments, $charset = 'utf-8') {
if (is_array($arguments)) {
$postData = json_encode($arguments);
} else {
$postData = $arguments;
} $ch = curl_init();
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length:' . strlen($postData)));
curl_setopt($ch, CURLOPT_TIMEOUT, self::$timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::$timeout); $returnValue = curl_exec($ch);
curl_close($ch);
if ($charset != 'utf-8') {
$returnValue = iconv($charset, 'utf-8', $returnValue);
}
return $returnValue;
} } $params = array(
'uid' => $uid,
'incr' => $incr,
'type' => $type,
'source' => $scorce,
'sign' => $sKey
);
$ob = new RemoteToolService();
$data =$ob->send($callUrl, 'post', $params);

对curl的了解还是不太清晰,只是暂时能用。。

curl get post 数据的更多相关文章

  1. PHP CURL模拟提交数据 攻击N次方

    public function actionCurl(){ $data['DATA']='{"NAME":"c","LEGEND":&quo ...

  2. 直接通过curl方式取得数据、模拟登陆、POST数据

    博客园的Markdown编辑器太坑爹了@!!! 算了.不用格式了!!! /********************** curl 系列 ***********************/ //直接通过c ...

  3. 使用PHP CURL的POST数据

    使用PHP CURL的POST数据 curl 是使用URL语法的传送文件工具,支持FTP.FTPS.HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP.cur ...

  4. AJAX+cURL+SimpleXMLElement处理数据

    curl_xml.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  5. 简单的curl抓取数据

    工欲善其事,必先利其器,数据抓取同样也是如此,PHP数据抓取常用CURL. CURL是一个使用libcurl库与各类服务器进行通讯,支持很多协议,如HTTP.FTP.TELNET等. curl_ini ...

  6. php通过curl发送XML数据,并获取XML数据

    php编程中经常会用到用xml格式传送数据,如调用微信等第三方接口经常用到,这里演示下php以curl形式发送xml,并通过服务器接收 一.发送xml数据 -- postXml.php <?ph ...

  7. php 使用 curl 发送 post 数据

    作为第三方开发商,经常会需要调用平台接口,远程调用,就要用到curl,其实质就是叫调用的方法与用到的参数以http post的方式发送至平台服务器. 简单的例子: $url = 'http://'; ...

  8. php 通过curl获取远程数据,返回的是一个数组型的字符串,高手帮忙如何将这个数组类型的字符串变成数组。

    如 Array([0] => Array([0] => Array([kd_status] => 已签收[kd_time] => 2014-04-30 18:59:43 [b] ...

  9. curl获得cookie数据<转>

    CURL *curl; CURLcode res; struct curl_slist *headers = NULL; curl_global_init(CURL_GLOBAL_ALL); curl ...

随机推荐

  1. TYVJ P1056 能量项链 Label:环状区间DP

    做题记录:2016-08-16 20:05:27 背景 NOIP2006 提高组 第一道 描述     在Mars星球上,每个Mars人都随身佩带着一串能量项链.在项链上有N颗能量珠.能量珠是一颗有头 ...

  2. HDU 4632 Palindrome subsequence(DP)

    题目链接 做的我很无奈,当时思路很乱,慌乱之中,起了一个想法,可以做,但是需要优化.尼玛,思路跑偏了,自己挖个坑,封榜之后,才从坑里出来,过的队那么多,开始的时候过的那么快,应该就不是用这种扯淡方法做 ...

  3. Java中实现文件上传下载的三种解决方案

    第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null ...

  4. 基于HTML5实现的超酷摄像头(HTML5 webcam)拍照功能 - photobooth.js

    在线演示 WebRTC可能是明年最受关注的HTML5标准了,Mozilla为此开发了一套帮助你控制硬件的API,例如,摄像头,麦克风,或者是加速表.你可以不依赖其它的插件来调用你需要的本机硬件设备. ...

  5. ThinkPHP几个配置文件的位置

    1.常用的ThinkPHP\Conf\convention.php 2.ThinkPHP/Lib/Behavior/ParseTemplateBehavior.class.php模板引擎相关配置

  6. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  7. PHP 设计模式 笔记与总结(9)数据对象映射模式

    [数据对象映射模式] 是将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作.例如在代码中 new 一个对象,使用数据对象映射模式就可以将对象的一些操作比如设置一些属性,就会自动保存到数 ...

  8. PHP常用正则表达式汇总 [复制链接]

    PHP常用正则表达式汇总 [复制链接] 上一主题下一主题   离线我是小猪头   法师     发帖 539 加关注 发消息 只看楼主 倒序阅读 使用道具楼主  发表于: 2011-06-22 更多 ...

  9. 使用 PHP 限制下载速度

    使用 PHP 限制下载速度 [来源] 达内    [编辑] 达内   [时间]2012-12-12 经常遇到一个问题,那就是有人再办公室下载东西,影响大家上网.办公.同样的问题,要是出现在了服务器上面 ...

  10. (转)maven 配置在eclipse中

    maven3 下载配置 下载地址 http://maven.apache.org/download.cgi 在线文档 http://maven.apache.org/ref/3.0.5/ 安装 一.安 ...