PHP-Curl模擬HTTPS請求


 使用PHP-Curl方式模擬HTTPS請求,測試接口傳參和返回值狀態
 
上代碼!!
<?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();

 

結果:

圖片


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM