PHP Curl请求Https接口

在请求http的时候只需要

file_get_contents("http://www.sojson.com/open/api/weather/json.shtml?city=$Position");就可以了,但是发现这个接口现在变成了https协议了还用这种方法就会403首先看看PHP有没有curl扩展,我是7.2

我用的是Laravel社区的封装好的方法
 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=北京");
 

原文地址:https://www.cnblogs.com/wlphp/p/8600945.html

时间: 2024-08-29 07:53:38

PHP Curl请求Https接口的相关文章

nginx+php下curl请求https报502错

在做公司项目的时候使用了第三方的API接口,且接口采用的是https请求,在本地的wamp集成环境开发测试正常,放到服务器上结果报错 nginx 502 bad gateway.在论坛中爬楼了几天今天终于找到原因,php版本问题: 公司项目线上环境: 服务器安装了wdcp其中nginx是1.4.2版本 php是5.2.17版本 部署项目上去后,怎么运行都报502错,刚开始怀疑是nginx配置问题,百度了许久说请求https需要ssl于是配置了nginx的ssl后问题依旧,无奈只好继续搜索答案.

php post 请求https接口

/** * POST请求https接口返回内容 * @param string $url [请求的URL地址] * @param string $post [请求的参数] * @return string */ public function post_curls($url, $post) { $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl,

curl 请求https内容,返回空

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$api); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//禁止直接显示获取的内容 重要 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // $json = curl_exec($ch);

带jsk证书,请求https接口

首先是三个返回的实体类 BaseVo.java package https2; import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; public class BaseVo implements Serializable { private static final long serial

php curl请求https 返回无结果|false|errno:35

1 SSL: certificate subject name 'WMSvc-GWAMSERVER02' does not match target host name 把curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 2 Curl error: SSL certificate problem: self signed certificate in certificat 这个是CURLOPT_SSL_VERIFYHOST 设为1引起,不同版本的l

php使用curl扩展请求HTTPS链接报sslv3 alert 错误

报错信息 使用php的curl请求https链接时报"error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure" 错误 原因分析 使用php的curl扩展时,curl_setop的CURLOPT_SSLVERSION取值为3,对应协议为ssl v3,因为之前的POODLE 病毒爆发,许多网站禁用了sslv3(nginx默认是禁用的,ssl_protocols 默认值为TLSv1 TLSv1.1 T

cxf webservice请求https

本地java请求https接口,不需要添加证书: 只需要修改配置文件applicationContext-soap-client.xml: <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http="http://cxf.apache.org/transports

jmeter压测https接口

jmeter下载完成后cmd进入jmeter\apache-jmeter-5.1.1\bin目录下,执行jmeter.bat,之后进入jmeter GUI页面 1. 创建http授权管理器 其中①:是https接口的url ②.③是登陆https接口登陆账户好密码 ④选择Basic_digest ⑤设置完后点击载入 2. 创建线程组 ①    压测的线程数量 ②   这些线程执行时间 ③   执行这些线程的轮数 3. 在创建的线程组中创建HTTP请求 ①   https接口的话这里一定要写为ht

PHP:CURL分别以GET、POST方式请求HTTPS协议接口api

1.curl以GET方式请求https协议接口 //注意:这里的$url已经包含参数了,不带参数你自己处理哦GET很简单 function curl_get_https($url){ $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1)