1. 程式人生 > >php cURL模擬https請求報錯

php cURL模擬https請求報錯

最近在做微信開發,通過cURL請求建立微信自定義選單的時候結果報錯,後來查資料總要解決。

環境:阿里 centos6.4 php5.3 nginx

報錯一:

Problem with the SSL CA cert (path? access…

解決方式:

1、不管之前安裝過沒有 直接在安裝ca-certificates下

yum install ca-certificates 
2、重啟php-fpm
service php-fpm restart
或者
/etc/init.d/php-fpm restart
3、設定 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);//curl https ssl ------------------------------------------------------------------------------

報錯二:

Protocol https not supported or disabled in libcurl

解決方式:

竟然是因為請求的URL前面多了一個空格 $url = " https://api.weixin.qq.com/cgi-bin/menu/create?";

於是trim()處理了下。

===============================================

/**
* get
* $url = "http://www.sxfenglei.com/get.php?name=小小馮同學&age=30";
*/
function getCurl($url=''){
	if(empty($url)){
		return false;
	} 
	$url = trim($url);
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);//設定不帶標頭檔案
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//設定獲取的資訊以檔案流的形式返回 
    $output = curl_exec($curl);  
	if($output === false){
		echo 'cURL Error:'.curl_error($curl);
	}
    curl_close($curl);
	return $output;
}
/**
* post
* $url = "http://www.sxfenglei.com/post.php";
* $data = array('name'=>'小小馮同學','age'=>30);
*/
function postCurl($url='',$data=array()){
	if(empty($url)){
		return false;
	} 
    $url = trim($url);
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_HEADER, 0); //設定不帶標頭檔案
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//設定獲取的資訊以檔案流的形式返回 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);//curl https ssl
    curl_setopt($curl, CURLOPT_POST, 1);//設定post方式提交 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //設定post資料
    $output = curl_exec($curl);  
	if($output === false){
		echo 'cURL Error:'.curl_error($curl);
	}
    curl_close($curl);
	return $output;
}