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

yii2 中 linslin\Curl的基本使用

时间:2022-10-21 11:55:45浏览次数:54  
标签: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://www.shanhubei.com/archives/2418.html

标签:http,curl,example,linslin,post,yii2,com,Curl
From: https://www.cnblogs.com/shanhubei/p/16812990.html

相关文章

  • Wget与Curl
    wget定义下载工具格式wgetoptionsurl使用方法wgeturl直接下载网页的内容wget-ofileurl会话返回信息保存在文件中wget-Ofileurl......
  • curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack
    脚本网不通,手工安装,下载安装包安装包下载┌──[[email protected]]-[~/awx]└─$https://github.com/kubernetes-sigs/kustomize/releases/download/kus......
  • PHP 中的CURL 模拟表单的post提交
    PHP中的CURL模拟表单的post提交废话不多说啦,直接上代码:<?php$data=['username'=>'乔峰','skill'=>'擒龙手'];$headers=array('Content-Type:application/x-www-form-......
  • 将Curl转为Python的方法
    怎样将Curl转为Python的方法作为一名后端程序员,在服务器调试的过程中,使用curl命令为我们调试接口带来了很多的方便,极大地提高了效率;如下可以实现Get请求:curl'http://ba......
  • MinGW编译libcurl
     1.下载源码 https://github.com/curl/curl/releases/tag/curl-7_61_1(我下载的是 curl-7.61.1.zip)2.使用qt编译工具(可以把这个bin添加到环境变量或者带着目录......
  • libcurl 0xC0000005: 读取位置 0x00006464 时发生访问冲突
    场景   长时间调用libcurl获取数据,异常崩溃,提示如下:0x7298464D (ucrtbased.dll) (yushivehicleservice.exe.dmp 中)处有未经处理的异常: 0xC0000005: 读取位置 0......
  • curl
     curl -i-k-XPOST"https://192.168.0.98:19234/hello"-H"Context-typeapplication/json"-d'{"type":"hello"}' --tlsv1.2      参考: ......
  • 快速抓取数据的方式,curl, uncurl, requests 好的!
    第一步:Chrome的接口右键,copycurl的脚本第二步:拷贝到shell下,执行,可以正常请求数据回来第三步:安装​​uncurl​​pip3installuncurl第四步:上面的命令用uncurl包裹一下un......
  • 解决curl下载夹带中文的文件
    1.提供代码重点关注curl_escapeAPI#include<stdlib.h>#include<stdio.h>#include<sys/stat.h>#include<curl/curl.h>size_tgetcontentlengthfunc(void*p......
  • curl查看请求是走http还https
    下面是http请求过程curl-vhttp://www.boyblue.xyz*Abouttoconnect()towww.boyblue.xyzport80(#0)*Trying192.168.1.28...*Connectedtowww.boyblue.x......