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

如何提高情商(EQ)

地方门户网站项目可行性分析

快乐来自于做出「舍弃」的能力

2009年即将流行的牛逼语录:经典的我都无语了(转载)

小清新必须死

SolveigMM Video Splitter - 无损视频编辑工具(视频剪辑,合并等)

《挪威的森林》精彩篇章:永远记得我

不要再Best Regards了!英文Email四大问题

聪明处事72招

没有灵魂,只有交易——为何“苹果”会杀人