扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

php濡備綍璁块棶https

扬州沐宇科技
2021-03-03 11:11:02
HTTPS, PHP

php濡備綍璁块棶https

鍦╬hp涓娇鐢╟url搴撹闂甴ttps锛屽叿浣撴柟娉曞涓嬶細

function curlPost($url, $data = array(), $timeout = 30, $CA = true){

$cacert = getcwd() . '/cacert.pem'; //CA鏍硅瘉涔?

$SSL = substr($url, 0, 8) == "https://" ? true : false;

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout-2);

if ($SSL && $CA) {

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 鍙俊浠籆A棰佸竷鐨勮瘉涔?

curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA鏍硅瘉涔︼紙鐢ㄦ潵楠岃瘉鐨勭綉绔欒瘉涔︽槸鍚︽槸CA棰佸竷锛?

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 妫€鏌ヨ瘉涔︿腑鏄惁璁剧疆鍩熷悕锛屽苟涓旀槸鍚︿笌鎻愪緵鐨勪富鏈哄悕鍖归厤

} else if ($SSL && !$CA) {

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 淇′换浠讳綍璇佷功

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 妫€鏌ヨ瘉涔︿腑鏄惁璁剧疆鍩熷悕

}

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //閬垮厤data鏁版嵁杩囬暱闂

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //data with URLEncode

$ret = curl_exec($ch);

//var_dump(curl_error($ch)); //鏌ョ湅鎶ラ敊淇℃伅

curl_close($ch);

return $ret;

}

扫码添加客服微信