Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26213324
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类:

2009-10-07 11:28:33

上午终于把IF接口设计好了,加密与解密认证通过客户认定。主要通过三个参数来识别验证。
下面将用到向远程服务器提交参数,并获取结果来处理。

先收集几种远程执行URL并取得结果的方法。晚上有时间就完成这个功能。
方法1: 用file_get_contents 以get方式获取内容

 

  1. $url=’’;  

  2. $html = file_get_contents($url);  
  3. //print_r($http_response_header);
  4. ec($html);  
  5. printhr();  

  6.    printarr($http_response_header);  

  7. printhr();  
  8.    ?> 

方法2: 用fopen打开url, 以get方式获取内容
我觉得这个方法比较常用。

 


  1. $fp = fopen($url, ‘r’);  

  2. printarr(stream_get_meta_data($fp));  

  3. printhr();  
  4. while(!feof($fp)) {  
  5. $result .= fgets($fp, 1024);  
  6. }  
  7. echo "url body:    $result";  
  8. printhr();  

  9. fclose($fp);  
  10. ?> 

方法3:用file_get_contents函数,以post方式获取url

  1. $data = array (’foo’ => ‘bar’);  
  2. $data = http_build_query($data);  

  3. $opts = array (  
  4. ‘http’ => array (  
  5. ‘method’ => ‘POST’,  
  6. ‘header’=> "Content-type: application/x-www-form-urlencoded\r\n" .  
  7. "Content-Length: " . strlen($data) . "\r\n",  
  8. ‘content’ => $data
  9. ),  
  10. );  

  11. $context = stream_context_create($opts);  
  12. $html = file_get_contents(’’, false, $context);  

  13. echo $html;  
  14. ?> 

方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body

  1. function get_url ($url,$cookie=false) {  
  2. $url = parse_url($url);  
  3. $query = $url[path]."?".$url[query];  
  4. ec("Query:".$query);  
  5. $fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);  
  6. if (!$fp) {  
  7. return false;  
  8. } else {  
  9. $request = "GET $query HTTP/1.1\r\n";  
  10. $request .= "Host: $url[host]\r\n";  
  11. $request .= "Connection: Close\r\n";  
  12. if($cookie) $request.="Cookie:   $cookie\n";  
  13. $request.="\r\n";  
  14.        fwrite($fp,$request);  
  15. while(!@feof($fp)) {  
  16. $result .= @fgets($fp, 1024);  
  17.        }  
  18.        fclose($fp);  
  19. return $result;  
  20. }  
  21. }  


  22. //获取url的html部分,去掉header
  23. function GetUrlHTML($url,$cookie=false) {  

  24. $rowdata = get_url($url,$cookie);  
  25. if($rowdata)  
  26.     {  
  27. $body= stristr($rowdata,"\r\n\r\n");  
  28. $body=substr($body,4,strlen($body));  
  29. return $body;  
  30.     }  

  31. return false;  

  32. }  

  33. ?> 
  1. function HTTP_Post($URL,$data,$cookie, $referrer="") {  

  2. // parsing the given URL
  3. $URL_Info=parse_url($URL);  

  4. // Building referrer
  5. if($referrer=="") // if not given use this script as referrer
  6. $referrer="111";  

  7. // making string from $data
  8. foreach($data as $key=>$value)  
  9. $values[]="$key=".urlencode($value);  
  10. $data_string=implode("&",$values);  

  11. // Find out which port is needed – if not given use standard (=80)
  12. if(!isset($URL_Info["port"]))  
  13. $URL_Info["port"]=80;  

  14. // building POST-request:
  15. $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";  
  16. $request.="Host: ".$URL_Info["host"]."\n";  
  17. $request.="Referer: $referer\n";  
  18. $request.="Content-type: application/x-www-form-urlencoded\n";  
  19. $request.="Content-length: ".strlen($data_string)."\n";  
  20. $request.="Connection: close\n";  

  21. $request.="Cookie:   $cookie\n";  

  22. $request.="\n";  
  23. $request.=$data_string."\n";  

  24. $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);  
  25. fputs($fp, $request);  
  26. while(!feof($fp)) {  
  27. $result .= fgets($fp, 1024);  
  28. }  
  29. fclose($fp);  

  30. return $result;  
  31. }  
  32. printhr();  
  33. ?> 

方法6:使用curl库,使用curl库之前,你可能需要查看一下.ini,查看是否已经打开了curl扩展

  1. $ch = curl_init();  
  2. $timeout = 5;  
  3. curl_setopt ($ch, CURLOPT_URL, ‘’);  
  4. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
  5. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
  6. $file_contents = curl_exec($ch);  
  7. curl_close($ch);  

  8. echo $file_contents;  
  9. ?> 

关于curl库
curl官方网站http://curl.haxx.se/
curl 是使用URL语法的传送文件工具,支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。curl 支持SSL证书、HTTP POST、HTTP PUT 、FTP 上传,kerberos、基于HTT格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道和大量其他有用的技巧,最近热门的 SNS中也用到这个方法,取得MSN上的好友列表等,应用还是挺多的。只不过需要组件支持,开启方法我的技术圈中有说 明:


  1. function printarr(array $arr)  
  2. {  
  3. echo "
    Row field count: ".count($arr)."
    ";  
  4. foreach($arr as $key=>$value)  
  5.     {  

  6. echo "$key=$value   
    ";  
  7.     }  
  8. }  
  9. ?> 
阅读(1860) | 评论(5) | 转发(0) |
0

上一篇:PHP设置页面编码

下一篇:js循环显示图片

给主人留下些什么吧!~~

chinaunix网友2009-10-29 14:41:07

今天尝到了将数据与显示分离带来的好处!不可言喻呀。以前要调整样式 还得重新生成数据。将数据与显示进行绑定到了一块这样非常不方便。 以后我思路就是数据是纯数据、而不管是显示

chinaunix网友2009-10-28 18:44:37

http://blog.csdn.net/beimuaihui/archive/2009/05/20/4202881.aspx

chinaunix网友2009-10-28 16:00:48

405不让我GET。有时间要整理一下。原来要设置一下HOST位置的就行!

chinaunix网友2009-10-28 15:44:19

采用curl库在PHP程序之间传递数组[原创] http://blog.s135.com/post/285/

chinaunix网友2009-10-28 15:09:50

同一域名对应多个IP时,PHP获取远程网页内容的函数[原创] http://blog.s135.com/post/389/#entrymore