类似于dreamhost这类主机服务商,是显示fopen的使用 的。使用php的curl可以实现支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。curl 支持SSL证书、HTTP POST、HTTP PUT 、FTP 上传,kerberos、基于HTT格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道就最常用的来说,是基于http的 get和post方法。
代码实现:
1、http的get实现
- $ch = curl_init("http://www.domain.com/api/index.php?test=1") ;
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ;
- curl_setopt($ch, CURLOPT_BINARYTRANSFER, true) ;
- echo $output = curl_exec($ch) ;
-
- $fh = fopen("out.html", ‘w‘) ;
- fwrite($fh, $output) ;
- fclose($fh) ;
2、http的post实现
- <?php
- $url = ‘http://www.domain.com/api/‘ ;
- $fields = array(
- ‘lname‘=>‘justcoding‘ ,
- ‘fname‘=>‘phplover‘ ,
- ‘title‘=>‘myapi‘,
- ‘age‘=>‘27‘ ,
- ‘email‘=>‘1353777303@gmail.com‘ ,
- ‘phone‘=>‘1353777303‘
- );
-
- $ch = curl_init() ;
- curl_setopt($ch, CURLOPT_URL,$url) ;
- curl_setopt($ch, CURLOPT_POST,count($fields)) ;
- curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
-
- ob_start();
- curl_exec($ch);
- $result = ob_get_contents() ;
- ob_end_clean();
-
- echo $result;
-
- curl_close($ch) ;
http://www.domain.com/api/index.php
- <?php
-
- if($_GET[‘test‘])
- {
- print_r($_GET);
- }
-
- if($_POST)
- {
- print_r($_POST);
- }
3. php的curl传送cookie
两种方式:
一种是自动:
- curl_setopt($curlHandle, CURLOPT_COOKIEJAR, ‘cookie.txt ‘);
- curl_setopt($curlHandle, CURLOPT_COOKIEFILE, ‘cookie.txt ‘);
这样COOKIE会自动跟上去.
不过要分两次,一是先访问产生cookie,接着连结才能用cookie
例子:
- <?php
-
- function get_curlcuconent2($filename,$referer)
- {
- $cookie_jar = tempnam(‘./tmp‘,‘JSESSIONID‘);
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $filename);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-
-
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
- $filecontent=curl_exec($ch);
- curl_close($ch);
-
- $ch = curl_init();
- $hostname ="www.domain.com";
-
- curl_setopt($ch, CURLOPT_URL, $filename);
- curl_setopt($ch, CURLOPT_REFERER, $referer);
- curl_setopt($ch, CURLOPT_USERAGENT, "www.domain.com");
-
-
-
-
-
-
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
- curl_setopt($ch, CURLOPT_HEADER, false);
- curl_setopt($ch, CURLOPT_GET, 1);
-
- $filecontent = curl_exec($ch);
- preg_match_all("/charset=(.+?)[NULL\"\‘]/is",$filecontent, $charsetarray);
- if(strtolower($charsetarray[1][0])=="utf-8")
- $filecontent=iconv( ‘utf-8‘, ‘gb18030//IGNORE‘ , $filecontent);
- curl_close($ch);
- return $filecontent;
- }
-
- ?>
一种自定义:
- $header[]= ‘Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, text/html, * ‘. ‘/* ‘;
- $header[]= ‘Accept-Language: zh-cn ‘;
- $header[]= ‘User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ‘;
- $header[]= ‘Host: ‘.$你的目标HOST;
- $header[]= ‘Connection: Keep-Alive ‘;
- $header[]= ‘Cookie: ‘.$你的COOKIE串;
-
- curl_setopt($curlHandel,CURLOPT_HTTPHEADER,$header);
PHP的curl实现get,post 和 cookie(几个实例),布布扣,bubuko.com
PHP的curl实现get,post 和 cookie(几个实例)
原文:http://www.cnblogs.com/kongxs/p/3832009.html