curl的POST请求,封装方法

//POST请求
//参数1是请求的url
//参数2是发送的数据的数组
//参数3是其他POST选项
public static function POST($url, array $post = array(), array $options = array(), $timeout = 10)
{
$defaults = array(
CURLOPT_POST => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => $timeout,
CURLOPT_POSTFIELDS => http_build_query($post),
CURLOPT_SSLVERSION => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 2
);

$ch = curl_init();
curl_setopt_array($ch, ($options + $defaults));

$result = curl_exec($ch);
if ($result === false)
{
Yii::log("POST ERROR:" . curl_error($ch));
}

curl_close($ch);

return $result;
}

猜你喜欢

转载自www.cnblogs.com/gramblog/p/9876889.html