discover_pingback_server_uri

Finds a pingback server URI based on the given URL.

Description

(false|string) discover_pingback_server_uri( (string) $url, (string) $deprecated = '' ); 

Checks the HTML for the rel="pingback" link and x-pingback headers. It does a check for the x-pingback headers first and returns that, if available. The check for the rel="pingback" has more overhead than just the header.

Returns (false|string)

False on failure, string containing URI on success.

Parameters (2)

0. $url (string)
URL to ping.
1. $deprecated — Optional. (string) => ''
The deprecated.

Usage

  1. if ( !function_exists( 'discover_pingback_server_uri' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment.php'; 
  3.  
  4. // URL to ping. 
  5. $url = ''; 
  6.  
  7. // The deprecated. 
  8. $deprecated = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = discover_pingback_server_uri($url, $deprecated); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/comment.php  
  1. function discover_pingback_server_uri( $url, $deprecated = '' ) { 
  2. if ( !empty( $deprecated ) ) 
  3. _deprecated_argument( __FUNCTION__, '2.7.0' ); 
  4.  
  5. $pingback_str_dquote = 'rel="pingback"'; 
  6. $pingback_str_squote = 'rel=\'pingback\''; 
  7.  
  8. /** @todo Should use Filter Extension or custom preg_match instead. */ 
  9. $parsed_url = parse_url($url); 
  10.  
  11. if ( ! isset( $parsed_url['host'] ) ) // Not a URL. This should never happen. 
  12. return false; 
  13.  
  14. //Do not search for a pingback server on our own uploads 
  15. $uploads_dir = wp_get_upload_dir(); 
  16. if ( 0 === strpos($url, $uploads_dir['baseurl']) ) 
  17. return false; 
  18.  
  19. $response = wp_safe_remote_head( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) ); 
  20.  
  21. if ( is_wp_error( $response ) ) 
  22. return false; 
  23.  
  24. if ( wp_remote_retrieve_header( $response, 'x-pingback' ) ) 
  25. return wp_remote_retrieve_header( $response, 'x-pingback' ); 
  26.  
  27. // Not an (x)html, sgml, or xml page, no use going further. 
  28. if ( preg_match('#(image|audio|video|model)/#is', wp_remote_retrieve_header( $response, 'content-type' )) ) 
  29. return false; 
  30.  
  31. // Now do a GET since we're going to look in the html headers (and we're sure it's not a binary file) 
  32. $response = wp_safe_remote_get( $url, array( 'timeout' => 2, 'httpversion' => '1.0' ) ); 
  33.  
  34. if ( is_wp_error( $response ) ) 
  35. return false; 
  36.  
  37. $contents = wp_remote_retrieve_body( $response ); 
  38.  
  39. $pingback_link_offset_dquote = strpos($contents, $pingback_str_dquote); 
  40. $pingback_link_offset_squote = strpos($contents, $pingback_str_squote); 
  41. if ( $pingback_link_offset_dquote || $pingback_link_offset_squote ) { 
  42. $quote = ($pingback_link_offset_dquote) ? '"' : '\''; 
  43. $pingback_link_offset = ($quote=='"') ? $pingback_link_offset_dquote : $pingback_link_offset_squote; 
  44. $pingback_href_pos = @strpos($contents, 'href=', $pingback_link_offset); 
  45. $pingback_href_start = $pingback_href_pos+6; 
  46. $pingback_href_end = @strpos($contents, $quote, $pingback_href_start); 
  47. $pingback_server_url_len = $pingback_href_end - $pingback_href_start; 
  48. $pingback_server_url = substr($contents, $pingback_href_start, $pingback_server_url_len); 
  49.  
  50. // We may find rel="pingback" but an incomplete pingback URL 
  51. if ( $pingback_server_url_len > 0 ) { // We got it! 
  52. return $pingback_server_url; 
  53.  
  54. return false;