我们通过对1. php curl的默认调用方法,get方式访问url
.... $ch = curl_init(); curl_setopt($ch, curlopt_httpheader, $header);//设置http头 curl_setopt($ch, curlopt_encoding, gzip );
//设置为客户端支持gzip压缩 curl_setopt($ch, curlopt_connecttimeout, 30 );
//设置连接等待时间 curl_setopt($ch, curlopt_url, $url ); curl_exec( $ch ); if ($error = curl_error($ch) ) { //出错处理 return -1; } fclose($fp); $curl_code = curl_getinfo($ch, curlinfo_http_code);
//获取http返回值 if( $curl_code == 200 ) { //正常访问url } //异常 ....
2. 设置http header支持php curl访问lighttpd服务器
$header[]= 'expect:';
3. 设置curl,只获取http header,不获取body:
curl_setopt($ch, curlopt_header, 1); curl_setopt($ch, curlopt_nobody, 1);
或者只获取body:
curl_setopt($ch, curlopt_header, 0);
// make sure we get the body curl_setopt($ch, curlopt_nobody, 0);
4. 访问虚拟主机,需设置host
$header[]= 'host: '.$host;
5. 使用post, put, delete等restful方式访问url
post: curl_setopt($ch, curlopt_post, 1 ); put, delete: curl_setopt($ch, curlopt_customrequest, delete);
//或者put,需要服务器支持这些方法。
6. php curl保存下载内容为文件
curl_setopt($ch, curlopt_file, $fp);
http://www.bkjia.com/phpjc/446438.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/446438.htmltecharticle我们通过对 1. php curl的默认调用方法,get方式访问url .... $ ch = curl_init (); curl_setopt($ch,curlopt_httpheader,$header);//设置http头 curl_setopt($ch,curlopt_...
