首页 > 其他分享 >yii2 中 linslin\Curl的基本使用

yii2 中 linslin\Curl的基本使用

时间:2023-02-18 10:02:32浏览次数:34  
标签:http curl example linslin post yii2 com Curl


 yii2 中 linslin\Curl的基本使用

一、get请求:

1.1 简单get请求

use linslin\yii2\curl;
$curl = new curl\Curl();

//get http://example.com/ get请求改网址
$response = $curl->get('http://example.com/');
// curl对象有errorCode和errorerrorText 属性,分别为错误代码和错误说明
if ($curl->errorCode === null) {
echo $response;
} else {
// 可以从这里获得所有的错误代码 https://curl.haxx.se/libcurl/c/libcurl-errors.html
switch ($curl->errorCode) {

case 6:
//host unknown example
break;
}
}

1.2、​​get 发起请求并且携带参数​

​GET request with GET params​

// GET request with GET params
// get 发起请求并且携带参数
// 例如 http://example.com/?key=value&scondKey=secondValue
$curl = new curl\Curl();
$response = $curl->setGetParams([
'key' => 'value',
'secondKey' => 'secondValue'
])
->get('http://example.com/');

二、POST请求

2.1、​​post 请求 , 数据格式为 form-urlencoded格式​

// POST URL form-urlencoded 
// post 请求 , 数据格式为 form-urlencoded格式
$curl = new curl\Curl();
$response = $curl->setPostParams([
'key' => 'value',
'secondKey' => 'secondValue'
])
->post('http://example.com/');

2.2、​​发起post请求,数据为json格式​

​POST RAW JSON​

// POST RAW JSON
// 发起post请求,数据为json格式
$curl = new curl\Curl();
$response = $curl->setRawPostData(
json_encode([
'key' => 'value',
'secondKey' => 'secondValue'
]))
->post('http://example.com/');

2.3、POST RAW XML发起post请求

数据为xml格式

// POST RAW XML
// 发起post请求,数据为xml格式
$curl = new curl\Curl();
$response = $curl->setRawPostData('<?xml version="1.0" encoding="UTF-8"?><someNode>Test</someNode>')
->post('http://example.com/');

2.4、​​设置header头部参数​

// POST with special headers
// 发起post请求,并且设置header头部参数
$curl = new curl\Curl();
$response = $curl->setPostParams([
'key' => 'value',
'secondKey' => 'secondValue'
])
->setHeaders([
'Custom-Header' => 'user-b'
])
->post('http://example.com/');

2.5、​​发起post请求,数据作为body以json串来传递,并且设置header头部参数​

// 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('http://example.com/');

2.6、​​Avanced POST request with curl options & error handling​

// 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('http://example.com/');
// 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);

标签:http,curl,example,linslin,post,yii2,com,Curl
From: https://blog.51cto.com/u_15967457/6065067

相关文章

  • Linux - curl 使用方法
    curl是强大的开源的数据传输工具,它支持很多协议,其中使用最多的是HTTP/HTTPS协议。curl使用libcurl库,很多编程语音都可以使用这个库1、curl基本使用curlblog.ling218......
  • curl post请求发送json数据两种方式(Window/Linux)
    curlpost请求发送json数据两种方式(Window/Linux) 设置请求头Content-Typecurl发送post请求,默认的content-type是:application/x-www-form-urlencoded。要发送json格式,......
  • libcurl 坑之 CURLOPT_WRITEFUNCTION
    参考blog:https://blog.csdn.net/szchtx/article/details/21740599   CURLOPT_WRITEFUNCTION回调回来的数据始终是不规则的json。服务器返回的是utf-8,使用win32......
  • 《用 libcurl 实现 HTTP 下载文件,其中设置了超时时间,以便在网络异常时能够快速中止下
    CURLOPT_TIMEOUT选项设置了超时时间为10秒,如果在这个时间内没有下载完成,则会返回一个超时错误,可以在回调函数中进行错误处理。如果网络异常或其他错误,也会在curl_eas......
  • wget 和 curl的区别
    原文https://www.codenong.com/s1190000022301195/https://geek-docs.com/linux/linux-ask-answer/difference-between-wget-vs-curl.htmlwget和curl是在没有任何GUI或......
  • 解决curl中文乱码问题
    文章目录​​1、问题描述​​​​2、解决方案:安装iconv​​1、问题描述curl下载地址:​​https://curl.se/download.html​​​在执行命令​​curlwww.baidu.com​​的时候......
  • Linux 上 libcurl库 curl_easy_perform Crash(signal 11 - SIGSEGV)
    PS:要转载请注明出处,本人版权所有。PS:这个只是基于《我自己》的理解,如果和你的原则及想法相冲突,请谅解,勿喷。前置说明  本文作为本人csdnblog的主站的备份。(BlogID......
  • Libcurl & Log4cplus 移植和使用 以及 Jsoncpp 简单使用
    PS:要转载请注明出处,本人版权所有。PS:这个只是基于《我自己》的理解,如果和你的原则及想法相冲突,请谅解,勿喷。前置说明  本文作为本人csdnblog的主站的备份。(BlogID......
  • #yyds干货盘点#Linux Curl 命令示例
        CURL是服务器上用于数据传输的命令行工具。CURL支持许多协议,例如:DICT,FILE,FTP,FTPS,GOPHER,HTTP,HTTPS,IMAP,IMAPS,LDAP,LDAPS,POP3,POP3S,RTM......
  • Curl 命令举例
    curl是一个强大的网络请求lib,虽然强大,但是参数太多哈,不得不吐槽,下面列举一下常用使用方法,供大家CVPOST请求入门:curl-XPOST"http://localhost:8020/upload"简单:cur......