PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/jsonrpc/public/services/phpolait/httpwrap.php

http://pyjamas.googlecode.com/
PHP | 112 lines | 59 code | 11 blank | 42 comment | 6 complexity | 05f4b80890cd1093a33175743639a213 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Wrapper for the appropriate HTTP post method for the system.
  5. *
  6. *
  7. *
  8. * PHP versions 4 and 5 (not tested under 5)
  9. *
  10. * LICENSE: LGPL
  11. *
  12. * @Release PHP-O-Lait Version 0.5
  13. *
  14. */
  15. /**
  16. * Wraps a CURL object.
  17. * Code from : http://www.faqts.com/knowledge_base/view.phtml/aid/15705/fid/6
  18. * Changed to set request as text/plain, and to work correctly with my data type.
  19. * @access public
  20. */
  21. class HttpWrapper_CURL {
  22. function post($URL, $data, $referrer="") {
  23. // parsing the given URL
  24. $URL_Info=parse_url($URL);
  25. // Building referrer
  26. if($referrer=="") // if not given use this script as referrer
  27. $referrer=$_SERVER["REQUEST_URI"];
  28. $ch = curl_init();
  29. curl_setopt($ch, CURLOPT_URL, $URL_Info["host"].$URL_Info["path"]);
  30. curl_setopt($ch, CURLOPT_HEADER, 0); // Don't return headers
  31. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return the result when curl_exec is called
  32. curl_setopt($ch, CURLOPT_REFERER, $referrer ); // The referrer
  33. curl_setopt($ch, CURLOPT_POST, 0); // We're doing a post call
  34. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Here's the post data
  35. $result=curl_exec ($ch);
  36. curl_close ($ch);
  37. return $result;
  38. }
  39. }
  40. /**
  41. * Native PHP Http Wrapper.
  42. * Basic code idea from: http://www.faqts.com/knowledge_base/view.phtml/aid/15705/fid/6
  43. * Changed to send request as text/plain, and return only the response body.
  44. *
  45. * @access public
  46. */
  47. class HttpWrapper_PHP {
  48. function post($URL, $data, $referrer="") {
  49. if($referrer=="") // if not given use this script as referrer
  50. $referrer=$_SERVER["SCRIPT_URI"];
  51. $URL_Info=parse_url($URL);
  52. if(!isset($URL_Info["port"]))
  53. $URL_Info["port"]=80;
  54. // building POST-request:
  55. $request.="POST ".$URL_Info["path"]." HTTP/1.0\n";
  56. $request.="Host: ".$URL_Info["host"]."\n";
  57. $request.="Referer: $referrer\n";
  58. $request.="Content-type: text/plain\n";
  59. $request.="Content-length: ".strlen($data)."\n";
  60. $request.="Connection: close\n";
  61. $request.="\n";
  62. $request.= $data ."\n";
  63. $result = "";
  64. $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);
  65. fputs($fp, $request);
  66. // We don't capture the HTTP Header
  67. $bCapturing = false;
  68. while(!feof($fp)) {
  69. $curline = fgets($fp, 4096);
  70. if ($bCapturing) {
  71. $result .= $curline;
  72. } elseif (strlen(trim($curline))==0) {
  73. $bCapturing=true;
  74. }
  75. }
  76. fclose($fp);
  77. /*
  78. echo "<b>HttpWrapper_PHP::post, Result = </b><br/><pre>";
  79. echo str_replace(array("<",">"),array("&lt;","&gt;"),$result);
  80. echo "</pre><hr>";
  81. */
  82. return $result;
  83. }
  84. }
  85. /**
  86. * Wrap HTTP Access.
  87. * Generates an appropriate wrapper for the system. If you would like to use a particular wrapper, just
  88. * modify this class method to return the appropriate wrapper for your system.
  89. *
  90. * @access protected
  91. */
  92. class HTTPWrapper {
  93. function GetWrapper() {
  94. if (extension_loaded("curl")) {
  95. return new HttpWrapper_CURL();
  96. } else {
  97. return new HttpWrapper_PHP();
  98. }
  99. }
  100. }
  101. ?>