PHP做接口中用到调用远程url的几种方法

来源:zhoz

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

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

 

  1. <?php  
  2. $url=’http://www.zhoz.com/’;  
  3. $html = file_get_contents($url);  
  4. //print_r($http_response_header);
  5. ec($html);  
  6. printhr();  
  7.    printarr($http_response_header);  
  8. printhr();  
  9.    ?> 

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

 

  1. <?php  
  2. $fp = fopen($url, ‘r’);  
  3. printarr(stream_get_meta_data($fp));  
  4. printhr();  
  5. while(!feof($fp)) {  
  6. $result .= fgets($fp, 1024);  
  7. }  
  8. echo "url body:    $result";  
  9. printhr();  
  10. fclose($fp);  
  11. ?> 

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

  1. <?php  
  2. $data = array (‘foo’ => ‘bar’);  
  3. $data = http_build_query($data);  
  4. $opts = array (  
  5. ‘http’ => array (  
  6. ‘method’ => ‘POST’,  
  7. ‘header’=> "Content-type: application/x-www-form-urlencoded\r\n" .  
  8. "Content-Length: " . strlen($data) . "\r\n",  
  9. ‘content’ => $data
  10. ),  
  11. );  
  12. $context = stream_context_create($opts);  
  13. $html = file_get_contents(‘http://localhost/e/admin/test.html’, false, $context);  
  14. echo $html;  
  15. ?> 

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

  1. <?php  
  2. function get_url ($url,$cookie=false) {  
  3. $url = parse_url($url);  
  4. $query = $url[path]."?".$url[query];  
  5. ec("Query:".$query);  
  6. $fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);  
  7. if (!$fp) {  
  8. return false;  
  9. } else {  
  10. $request = "GET $query HTTP/1.1\r\n";  
  11. $request .= "Host: $url[host]\r\n";  
  12. $request .= "Connection: Close\r\n";  
  13. if($cookie) $request.="Cookie:   $cookie\n";  
  14. $request.="\r\n";  
  15.        fwrite($fp,$request);  
  16. while(!@feof($fp)) {  
  17. $result .= @fgets($fp, 1024);  
  18.        }  
  19.        fclose($fp);  
  20. return $result;  
  21. }  
  22. }  
  23. //获取url的html部分,去掉header
  24. function GetUrlHTML($url,$cookie=false) {  
  25. $rowdata = get_url($url,$cookie);  
  26. if($rowdata)  
  27.     {  
  28. $body= stristr($rowdata,"\r\n\r\n");  
  29. $body=substr($body,4,strlen($body));  
  30. return $body;  
  31.     }  
  32. return false;  
  33. }  
  34. ?> 

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

  1. <?php  
  2. function HTTP_Post($URL,$data,$cookie, $referrer="") {  
  3. // parsing the given URL
  4. $URL_Info=parse_url($URL);  
  5. // Building referrer
  6. if($referrer=="") // if not given use this script as referrer
  7. $referrer="111";  
  8. // making string from $data
  9. foreach($data as $key=>$value)  
  10. $values[]="$key=".urlencode($value);  
  11. $data_string=implode("&",$values);  
  12. // Find out which port is needed – if not given use standard (=80)
  13. if(!isset($URL_Info["port"]))  
  14. $URL_Info["port"]=80;  
  15. // building POST-request:
  16. $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";  
  17. $request.="Host: ".$URL_Info["host"]."\n";  
  18. $request.="Referer: $referer\n";  
  19. $request.="Content-type: application/x-www-form-urlencoded\n";  
  20. $request.="Content-length: ".strlen($data_string)."\n";  
  21. $request.="Connection: close\n";  
  22. $request.="Cookie:   $cookie\n";  
  23. $request.="\n";  
  24. $request.=$data_string."\n";  
  25. $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);  
  26. fputs($fp, $request);  
  27. while(!feof($fp)) {  
  28. $result .= fgets($fp, 1024);  
  29. }  
  30. fclose($fp);  
  31. return $result;  
  32. }  
  33. printhr();  
  34. ?> 

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

  1. <?php  
  2. $ch = curl_init();  
  3. $timeout = 5;  
  4. curl_setopt ($ch, CURLOPT_URL, ‘http://www.zhoz.com/’);  
  5. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
  6. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
  7. $file_contents = curl_exec($ch);  
  8. curl_close($ch);  
  9. echo $file_contents;  
  10. ?> 

关于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上的好友列表等,应用还是挺多的。只不过需要组件支持,开启方法我的技术圈中有说明:http://o.zhoz.com/

  1. <?php  
  2. function printarr(array $arr)  
  3. {  
  4. echo "<br> Row field count: ".count($arr)."<br>";  
  5. foreach($arr as $key=>$value)  
  6.     {  
  7. echo "$key=$value    <br>";  
  8.     }  
  9. }  
  10. ?> 


评论权限被关闭.



赞助商

文章索引模板

好友推荐链接

强力推荐链接

分类目录

   

统计信息

Translator

Chinese (Simplified) flagItalian flagKorean flagChinese (Traditional) flagPortuguese flagEnglish flagGerman flagFrench flagSpanish flagJapanese flagArabic flagRussian flagGreek flagDutch flagBulgarian flagCzech flag
Croatian flagDanish flagFinnish flagPolish flagSwedish flagNorwegian flag          

标签

专利战 世界 中国 为什么 介绍 使用 公司 分析 利用 功能 原谅我红尘颠倒 发现 天涯 如何 实现 工具 应该 慕容雪村 技术 插件 搜索引擎 支持 数据库 文件 方式 时间 服务器 用户 简单 管理 系统 网站 美国 谁的心不曾柔软 进行 部分 问题 AJAX Google LAN Linux MySQL PHP plugin WordPress

热门浏览