/**
* curl get
*/
public function httpGet( $url ) {
$oCurl = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
//curl_setopt($oCurl, CURLOPT_VERBOSE, 1);
curl_setopt($oCurl, CURLOPT_HEADER, 0);
$sContent = curl_exec($oCurl);
// $aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
return $sContent;
}
/**
* 以post方式提交到对应的接口url
*/
public function postCurl( $postdata, $url, $second = 30) {
$ch = curl_init();
if(stripos($url,"https://")!==FALSE){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
//设置超时
curl_setopt( $ch, CURLOPT_TIMEOUT, $second );
curl_setopt( $ch, CURLOPT_URL, $url );
//设置header
curl_setopt( $ch, CURLOPT_HEADER, FALSE );
//要求结果为字符串且输出到屏幕上
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
//post提交方式
curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
//运行curl
$data = curl_exec( $ch );;
//返回结果
if ( $data ) {
curl_close( $ch );
return $data;
} else {
$error = curl_errno( $ch );
curl_close( $ch );
throw new \Exception( "curl errno: $error" );
}
}
网友评论