PHP中调用curl get 和 curl post 方法的封装

/**

* ***************************************************

* CURL GET 请求

*

* @param string $url

* **************************************************

*/

function curlGet($url, $timeout = 5)

{

$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_TIMEOUT, $timeout);

$sContent = curl_exec($oCurl);

$aStatus = curl_getinfo($oCurl);

curl_close($oCurl);

if (intval($aStatus["http_code"]) == 200) {

return $sContent;

} else {

return false;

}

}

/**

* curl POST 请求

*

* @param string $url

* [description]

* @param string $param

* [description]

* @param boolean $post_file

* $post_file 是否文件上传

* @return string content [description]

*/

function curlPost($url, $param, $post_file = false, $timeout = 5)

{

$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

}

if (is_string($param) || $post_file) {

$strPOST = $param;

} else {

$aPOST = array();

foreach ($param as $key => $val) {

$aPOST[] = $key . "=" . urlencode($val);

}

$strPOST = join("&", $aPOST);

}

curl_setopt($oCurl, CURLOPT_URL, $url);

curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($oCurl, CURLOPT_TIMEOUT, $timeout);

curl_setopt($oCurl, CURLOPT_POST, true);

curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);

$sContent = curl_exec($oCurl);

$aStatus = curl_getinfo($oCurl);

curl_close($oCurl);

if (intval($aStatus["http_code"]) == 200) {

return $sContent;

} else {

return false;

}

}

评论

Popular Posts

随身WiFi折腾指南: 解锁、刷机与DIY

Cromite - 极度注重隐私的Chromium浏览器

《赤い涙 》灼眼的夏娜剧场版插曲

求职面试时常被问到的65个问题与技巧性回答

伤不起的盖茨比——那些年,我们没逆袭到的女神

人类的十个有趣心理效应

电话销售技巧8:如何绕过前台

Alexa互联网信息公司中文官方网站

拒绝道德绑架,这才是真正的公益广告

感情的瓶颈期或者说磨合期该怎么度过?