• yii2.0 curl的使用


    yii2 curl的使用办法

    get:

    use linslinyii2curl;
    public function actionCurl($value =0)
    {
        $url = 'http://yandex.ru/search/';
        $curl = new curlCurl();
        //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 curlCurl();
    $response = $curl->setPostParams([
            'key' => 'value',
            'secondKey' => 'secondValue'
         ])
         ->post($authUrl);
    // POST RAW JSON
    // 发起post请求,数据为json格式
    $curl = new curlCurl();
    $response = $curl->setRawPostData(
         json_encode([
            'key' => 'value',
            'secondKey' => 'secondValue'
         ]))
         ->post($authUrl);
    // POST RAW XML
    / 发起post请求,数据为xml格式
    $curl = new curlCurl();
    $response = $curl->setRawPostData('<?xml version="1.0" encoding="UTF-8"?><someNode>Test</someNode>')
         ->post($authUrl);
    // POST with special headers
    // 发起post请求,并且设置header头部参数
    $curl = new curlCurl();
    $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 curlCurl();
    
    $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 curlCurl();
    
    $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);
  • 相关阅读:
    some requirement checks failed
    FTP下载文件时拒绝登陆申请怎么办?
    Linux查看与设定别名
    如何编写shell脚本
    Linux shell是什么
    Linux命令大全之查看登陆用户信息
    Linux命令大全之挂载命令
    论第二次作业之输入输出格式怎么合格(才疏学浅说的不对轻点喷我)
    文件词数统计
    软件工程作业--第一周
  • 原文地址:https://www.cnblogs.com/l-zl/p/7148904.html
Copyright © 2020-2023  润新知