Guest User

Untitled

a guest
Mar 20th, 2011
327
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. function test_it($uri)
  4. {
  5. //$handle = curl_init($uri);
  6. $handle = curl_init();
  7. curl_setopt( $handle, CURLOPT_URL, $uri);
  8. $theHeaders = '';
  9.  
  10. #
  11. # The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
  12. # {in WP $timeout }
  13. curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT, 5000);
  14.  
  15. #
  16. # The maximum number of seconds to allow cURL functions to execute.
  17. # {in WP treated as the same as $timeout }
  18. curl_setopt( $handle, CURLOPT_TIMEOUT, 5000);
  19.  
  20. #
  21. # The number of milliseconds to wait while trying to connect. Use 0 to wait indefinitely.
  22. # If libcurl is built to use the standard system name resolver, that portion of the connect
  23. # will still use full-second resolution for timeouts with a minimum timeout allowed of one second.
  24. # {can not be set in WP}
  25. //curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT_MS, 5000);
  26.  
  27. #
  28. # The number of seconds to keep DNS entries in memory. This option is set to 120 (2 minutes) by default.
  29. # {can not be set in WP}
  30. //curl_setopt( $handle, CURLOPT_DNS_CACHE_TIMEOUT );
  31.  
  32. # The contents of the "Accept-Encoding: " header. This enables decoding of the response.
  33. # Supported encodings are "identity", "deflate", and "gzip". If an empty string, "", is set,
  34. # a header containing all supported encoding types is sent.
  35. #
  36. //curl_setopt( $handle, CURLOPT_ENCODING, '');
  37.  
  38. //curl_setopt( $handle, CURLOPT_TRANSFERTEXT, false);
  39.  
  40. //curl_setopt( $handle, CURLOPT_BINARYTRANSFER, false);
  41.  
  42. # TRUE to be completely silent with regards to the cURL functions.
  43. //curl_setopt( $handle, CURLOPT_MUTE, false);
  44.  
  45. //curl_setopt( $handle, CURLOPT_VERBOSE, false);
  46.  
  47. #
  48. # 1 to check the existence of a common name in the SSL peer certificate. 2 to check the existence of
  49. # a common name and also verify that it matches the hostname provided.
  50. # {in WP $ssl_verify} {i assume false will be 0 and true will be treated as ?}
  51. curl_setopt( $handle, CURLOPT_SSL_VERIFYHOST, 1 );
  52.  
  53. #
  54. # FALSE to stop cURL from verifying the peer's certificate. Alternate certificates to verify against can
  55. # be specified with the CURLOPT_CAINFO option or a certificate directory can be specified with the
  56. # CURLOPT_CAPATH option. CURLOPT_SSL_VERIFYHOST may also need to be TRUE or FALSE if CURLOPT_SSL_VERIFYPEER
  57. # is disabled (it defaults to 2).
  58. # {in WP also $ss_verify}
  59. curl_setopt( $handle, CURLOPT_SSL_VERIFYPEER, false );
  60.  
  61. #
  62. # Autoreferr set by Curl
  63. # {in WP not used as they have their own default string}
  64. //curl_setopt( $handle, CURLOPT_AUTOREFERER, false);
  65.  
  66. #
  67. # The contents of the "User-Agent: " header to be used in a HTTP request.
  68. # {in WP user-agent filter or parameter}
  69. curl_setopt( $handle, CURLOPT_USERAGENT, 'WP-Favicon/0.5.1 (http://wordpress.org/extend/plugins/wp-favicons/; http://www.favcollector.com/)');
  70.  
  71. #
  72. # The contents of the "Referer: " header to be used in a HTTP request.
  73. # {not set in WP}
  74. //curl_setopt( $handle, CURLOPT_REFERER, '');
  75.  
  76. # Bitmask of CURLPROTO_* values. If used, this bitmask limits what protocols libcurl may use in a transfer
  77. # that it follows to in a redirect when CURLOPT_FOLLOWLOCATION is enabled. This allows you to limit specific
  78. # transfers to only be allowed to use a subset of protocols in redirections. By default libcurl will allow all
  79. # protocols except for FILE and SCP. This is a difference compared to pre-7.19.4 versions which unconditionally
  80. # would follow to all protocols supported. See also CURLOPT_PROTOCOLS for protocol constant values.
  81. # {not used in WP though would be handy to exclude protocols for favicons}
  82. //curl_setopt( $handle, CURLOPT_REDIR_PROTOCOLS, '')
  83.  
  84. #
  85. # The maximum amount of HTTP redirections to follow. Use this option alongside CURLOPT_FOLLOWLOCATION.
  86. # { in WP redirection filter or parameter by default 5}
  87. curl_setopt( $handle, CURLOPT_MAXREDIRS, 0 );
  88.  
  89. #
  90. # TRUE to follow any "Location: " header that the server sends as part of the HTTP header
  91. # (note this is recursive, PHP will follow as many "Location: " headers that it is sent,
  92. # unless CURLOPT_MAXREDIRS is set).
  93. # { in WP not yet: see: https://core.trac.wordpress.org/ticket/16855#comment:19}
  94. # { in our case set this to false if redirection is set to 0 }
  95. curl_setopt( $handle, CURLOPT_FOLLOWLOCATION, false );
  96.  
  97. #
  98. # TRUE to fail silently if the HTTP code returned is greater than or equal to 400. The
  99. # default behavior is to return the page normally, ignoring the code.
  100. # {not set in WP, it will handle errors itself in class-http.php lines 1410+}
  101. //curl_setopt( $handle, CURLOPT_FAILONERROR, false);
  102.  
  103. #
  104. # TRUE to exclude the body from the output. Request method is then set to HEAD.
  105. # Changing this to FALSE does not change it to GET.
  106. # {set in WP when called by wp_remote_head or with a parameter 'HEAD' then this is set to false}
  107. curl_setopt( $handle, CURLOPT_NOBODY, false );
  108.  
  109. #
  110. # TRUE to include the header in the output. (wp's blocking versus non blocking)
  111. # {in WP this is true when the 'blocking' parameter is set, which is the default}
  112. //curl_setopt( $handle, CURLOPT_HEADER, true);
  113.  
  114. #
  115. # TRUE to track the handle's request string.
  116. # {not set in WP though handy for debugging favicon code}
  117. //curl_setopt( $handle, CURLINFO_HEADER_OUT, true);
  118.  
  119. #
  120. # An array of HTTP header fields to set, in the format array('Content-type: text/plain', 'Content-length: 100')
  121. # To reset it it needs to be set to array
  122. # {in WP set as headers array parameter}
  123. //curl_setopt( $handle, CURLOPT_HTTPHEADER, array());
  124.  
  125. #
  126. # CURL_HTTP_VERSION_NONE (default, lets CURL decide which version to use),
  127. # CURL_HTTP_VERSION_1_0 (forces HTTP/1.0), or CURL_HTTP_VERSION_1_1 (forces HTTP/1.1).
  128. # { in WP default set to 1.0 }
  129. // curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE );
  130. curl_setopt( $handle, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
  131.  
  132. #
  133. # TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
  134. # FALSE output it directly
  135. # {in WP always true to parse it in fixed HTTP API format}
  136. curl_setopt( $handle, CURLOPT_RETURNTRANSFER, true );
  137.  
  138. $theResponse = curl_exec( $handle );
  139.  
  140. if ( !empty($theResponse) ) {
  141. echo 'there was a response' . "<br />";
  142. $headerLength = curl_getinfo($handle, CURLINFO_HEADER_SIZE);
  143. $theHeaders = trim( substr($theResponse, 0, $headerLength) );
  144. if ( strlen($theResponse) > $headerLength )
  145. $theBody = substr( $theResponse, $headerLength );
  146. else
  147. $theBody = '';
  148. if ( false !== strpos($theHeaders, "\r\n\r\n") ) {
  149. $headerParts = explode("\r\n\r\n", $theHeaders);
  150. $theHeaders = $headerParts[ count($headerParts) -1 ];
  151. }
  152. } else {
  153. echo 'there was no response <br />';
  154. if ( $curl_error = curl_error($handle) )
  155. echo $curl_error . "<br />";
  156. if ( in_array( curl_getinfo( $handle, CURLINFO_HTTP_CODE ), array(301, 302) ) )
  157. echo 'Too many redirects.' . "<br >" ;
  158. $theHeaders = array( 'headers' => array(), 'cookies' => array() );
  159. $theBody = '';
  160. }
  161. echo $theHeaders;
  162. echo $theBody;
  163. echo curl_getinfo( $handle, CURLINFO_HTTP_CODE );
  164. }
  165. test_it('http://www.informationweek.com/story/');
RAW Paste Data