CURL方式:
public function httpCurl($url,$data) {
$curl = curl_init ();
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt ( $curl, CURLOPT_URL, $url );
if (!empty($data))
{
curl_setopt ( $curl, CURLOPT_POST, 1 );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
}
$res = curl_exec ( $curl );
curl_close ( $curl );
return $res;
}
数据流方式:
/**
*Huang Jing
*2016年1月12日 10:51:29
*Http 客户端 实例
*Post方法
******/
class HttpClient {
//post方法提交
public static function post2($url, $data) { //file_get_content
$postdata = http_build_query( $data );
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded;charset=GBK',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents($url, false, $context);
return $result;
}
}
发表评论