话不多说,直接上代码
<?php
/**
 * 模拟post进行url请求
 * @param string $url
 * @param array $postdata
 */
function request_post($url = '', $postdata = []) {
     if (empty($url)) {
         return false;
     }
     if ($postdata != []) {
          $vars = http_build_query($postdata, '', '&'); 
        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 = 'https://www.sojson.com/open/api/weather/json.shtml?city=北京';
    $res = request_post($url);
    print_r($res);
}
testaction();

 



结果:

 

 php技术交流qq群(各个大佬在线解答技术问题): 953618831