• laravel curl post json


    <?php
    namespace AppBO;

    use IlluminateSupportFacadesDB;
    use IlluminateSupportFacadesLog;
    use CurlCurl;

    class Http
    {
    public function __construct()
    {
    }

    public function post($url, $params) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

    $post_result = curl_exec($ch);
    curl_close($ch);
    return $post_result;
    }

    public function postJson($url, $data_string) {
    $ch = curl_init();
    //$data_string = http_build_query($arr);
    //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt ( $ch, CURLOPT_URL, $url );
    curl_setopt ( $ch, CURLOPT_POST, 1 );
    curl_setopt($ch, CURLOPT_HEADER, 0);
    //curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
    //curl_setopt($curl, CURLOPT_TIMEOUT, 60000);
    //curl_setopt($curl, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书和hosts
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    //curl_setopt($curl, CURLOPT_PROXY, "127.0.0.1");
    //curl_setopt($curl, CURLOPT_PROXYPORT, 8999);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string)
    ));

    $post_result = curl_exec($ch);
    if (curl_errno($ch)) {
    //print curl_error($ch);
    }
    curl_close($ch);
    return $post_result;
    }

    public function get($url) {
    try {
    if (!isset($url))
    return -1;

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 1);
     
    //curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($param));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    //curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));

    Log::info('url:'.$url);
    $result = curl_exec($curl);
    curl_close($curl);
    //print_r($result);

    return $result;
    }
    catch (Exception $ex) {
    Log::info($ex);
    return -1;
    }
    }
    }
  • 相关阅读:
    数据仓库的一些理解(转)
    常见ETL工具一览,你知多少?
    dhcpv6开源软件配置
    svn
    js原型模式
    combo扩展:禁止手工改变输入框的值
    SQLite错误总结 error code 19: constraint failed
    ntp源码解读(一)
    ntp-keygen.c
    6.2.2认证
  • 原文地址:https://www.cnblogs.com/yuanxiaoping_21cn_com/p/8532850.html
Copyright © 2020-2023  润新知