yii2 curl的使用办法

get:

use linslin\yii2\curl;
public function actionCurl($value =0)
{
$url = 'http://yandex.ru/search/';
$curl = new curl\Curl();
//post http://example.com/, reset request before
$response = $curl->reset()->setOption(
CURLOPT_POSTFIELDS,
http_build_query(array(
'text' => $value
)
))->post($url);
return $curl->response;
//get
$authUrl = ‘http://www.baidu.com’;
$curl = new Curl();
$response = $curl->get($authUrl);
}

当访问https是可能会出现空白需要将ssl设置为false

$curl->setOption(CURLOPT_SSL_VERIFYPEER, false);

post:

// POST URL form-urlencoded
// post 请求 , 数据格式为 form-urlencoded格式
$curl = new curl\Curl();
$response = $curl->setPostParams([
'key' => 'value',
'secondKey' => 'secondValue'
])
->post($authUrl);
// POST RAW JSON
// 发起post请求,数据为json格式
$curl = new curl\Curl();
$response = $curl->setRawPostData(
json_encode([
'key' => 'value',
'secondKey' => 'secondValue'
]))
->post($authUrl);
// POST RAW XML
/ 发起post请求,数据为xml格式
$curl = new curl\Curl();
$response = $curl->setRawPostData('<?xml version="1.0" encoding="UTF-8"?><someNode>Test</someNode>')
->post($authUrl);
// POST with special headers
// 发起post请求,并且设置header头部参数
$curl = new curl\Curl();
$response = $curl->setPostParams([
'key' => 'value',
'secondKey' => 'secondValue'
])
->setHeaders([
'Custom-Header' => 'user-b'
])
->post($authUrl);
// POST JSON with body string & special headers
// 发起post请求,数据作为body以json串来传递,并且设置header头部参数
$curl = new curl\Curl(); $params = [
'key' => 'value',
'secondKey' => 'secondValue'
]; $response = $curl->setRequestBody(json_encode($params))
->setHeaders([
'Content-Type' => 'application/json',
'Content-Length' => strlen(json_encode($params))
])
->post($authUrl);
// Avanced POST request with curl options & error handling
post请求,设置参数
$curl = new curl\Curl(); $params = [
'key' => 'value',
'secondKey' => 'secondValue'
]; $response = $curl->setRequestBody(json_encode($params))
->setOption(CURLOPT_ENCODING, 'gzip')
->post($authUrl);
// List of status codes here http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
switch ($curl->responseCode) { case 'timeout':
//timeout error logic here
break; case 200:
//success logic here
break; case 404:
//404 Error logic here
break;
}
//list response headers
var_dump($curl->responseHeaders);

最新文章

  1. Unity 组件不常用知识备注
  2. PHP 判断点是否在多边形内
  3. DOM节点属性
  4. Linux find 用法示例
  5. Swift: 比较Swift中闭包传值、OC中的Block传值
  6. java 中Session 持久化问题
  7. 【WEB前端】CSS继承性和层叠性(极度重要)
  8. How to read the HTML DTD
  9. [BigData]关于Hadoop学习笔记第三天(PPT总结)(一)
  10. UPUPW PHP环境集成包
  11. Jquery Ajax 调用 WebService
  12. 手写总结:synchronized 和 lock 区别
  13. 【开发手记一】老生常谈:简简单单配置ZED板开发环境
  14. Spring Boot 2.X 如何优雅的解决跨域问题?
  15. git reset与git revert的区别
  16. 从零开始写自己的PHP框架系列教程[前言]
  17. udp重发
  18. Oracle和Mysql语法异同整理笔记
  19. jQuery CVE-2019-11358原型污染漏洞分析和修复建议
  20. windows环境下安装Anaconda(Python)

热门文章

  1. deconv的弊端
  2. Java 8新特性解读
  3. 【总结】mybatis分页(实现 + 原理)
  4. java的“同一”与“相等”
  5. centos8 curl: (35) error:141A318A:SSL routines:tls_process_ske_dhe:dh key too small
  6. LoRaWAN和LoRa的区别在那里?
  7. 【轻松学编程】如何快速学会一门高级编程语言,以python为例
  8. vue API 知识点(1)---全局 API 总结
  9. leetcode99:n-queens
  10. Docker系列02—Docker 网络模式