PHP-Curl simulates HTTPS request (code example)

藏色散人
Release: 2023-04-08 11:08:01
forward
4291 people have browsed it

PHP-Curl simulates HTTPS request (code example)

Use PHP-Curl to simulate HTTPS requests and test the interface parameters and return value status

Upload the code! !

<?php
/**
 * 模拟post进行url请求
 * @param string $url
 * @param array $postData
 */
function request_post($url = &#39;&#39;, $postData = []) {
     if (empty($url)) {
         return false;
     }
     if ($postData != []) {
          $vars = http_build_query($postData, &#39;&#39;, &#39;&&#39;); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
     } 
     $postUrl = $url;
     //初始化curl //转义
     $ch = curl_init();            
     //抓取指定网页 
     curl_setopt($ch, CURLOPT_URL,$postUrl);
     //设置header 
     curl_setopt($ch, CURLOPT_HEADER, 0);
     //要求结果为字符串且输出到屏幕上 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //规避SSL验证
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    //跳过HOST验证
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     //运行curl
     $data = curl_exec($ch); 
     curl_close($ch);
     return $data;
}
/**
 * 测试
 * @param string $url
 */
function testAction() {
     $url = &#39;https://www.sojson.com/open/api/weather/json.shtml?city=北京&#39;;
    $res = request_post($url);
    print_r($res);
}
testAction();
Copy after login

Result:

PHP-Curl simulates HTTPS request (code example)

For more related php knowledge, please visit php tutorial!

The above is the detailed content of PHP-Curl simulates HTTPS request (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template