|
发表于 2016-3-10 18:40:30
|
显示全部楼层
/**
* 创建一个CURL网络访问
*
* @param string $url 要访问的地址
* @param array $data 要POST的值
* @param array $headers 要附加的HTTP头信息
*
* @return string 返回服务器的响应, false请求失败
*/
function curlRequest($url = '', $data = array(), $headers = array())
{
if( empty( $url ) )
return false;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
if( $headers )
{
$temp = array();
foreach( $headers as $key => $val )
$temp[] = "{$key}: {$val}";
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $temp );
}
if( $data )
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
$output = curl_exec($ch);
curl_close($ch);
$startVar = substr( $output, 0, 1 );
$endVar = substr( $output, strlen($output) - 1, 1 );
if( $startVar == '{' && $endVar == '}' || $startVar == '[' && $endVar == ']' )
{
$temp = @json_decode( $output, true );
if( $temp !== false )
$output = $temp;
}
return $output;
} |
|