收藏文章 楼主

php curl_exec访问远程的https接口

版块:php   类型:普通   作者:小绿叶技术博客   查看:1821   回复:0   获赞:0   时间:2021-07-21 10:58:34


PHP Curl请求Https接口:  https://www.cnblogs.com/wlphp/p/8600945.html




<?php

#$url="eisc.cn"

public static function curl($url, $params = false, $ispost = 0, $https = 0)

    {

        $httpInfo = array();

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);

        curl_setopt($ch, CURLOPT_TIMEOUT, 30);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        if ($https) {

            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查

            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在

        }

        if ($ispost) {

            curl_setopt($ch, CURLOPT_POST, true);

            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

            curl_setopt($ch, CURLOPT_URL, $url);

        } else {

            if ($params) {

                if (is_array($params)) {

                    $params = http_build_query($params);

                }      

                curl_setopt($ch, CURLOPT_URL, $url . '?' . $params);  // 此处就是参数的列表,给你加了个?

            } else {

                curl_setopt($ch, CURLOPT_URL, $url);

            }

        }


        $response = curl_exec($ch);


        if ($response === FALSE) {

            //echo "cURL Error: " . curl_error($ch);

            return false;

        }

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        $httpInfo = array_merge($httpInfo, curl_getinfo($ch));

        curl_close($ch);

        return $response;

    }// 发送请求 $result = self::curl('网址', '参数', true); // 收到的数据需要转化一下 $json = json_decode($result);我的用法是,应为我调用的是天气预报的接口

$result= Curl::curl($url,"city=北京");


?>

提供企业建站服务,免费网防系统,提交信息登录 http://yundun.ddoss.cn 邮箱: proposal@ddoss.cn 
回复列表
默认   热门   正序   倒序

回复:php curl_exec访问远程的https接口

头像

用户名:

粉丝数:

签名:

资料 关注 好友 消息