Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Jak w PHP wysłać informacje przez XML-RPC
Forum PHP.pl > Forum > PHP
kom1nek
Hej. Chciałem napisać funkcję która wyśle mi ping do serwisu pingomatic.com lub poprostu google przez xml-rpc. Niestety nie wiem jak się do tego zabrać ;/. Bawił się tym ktoś kiedys ?
amii
Sam korzystam z tego. Do funkcji przekazujesz tytuł bloga i adres url. W pliku pinglist.txt musisz mieć adresy, które chcesz pingować, każdy w osobnej linii.

  1. function ping($blogTitle, $blogUrl) {
  2. // Please, edit these variables to your needs
  3.  
  4. //$blogTitle="tytuł!";
  5. //$blogUrl="http://www.adres.pl/all.html";
  6. $pingListFile="pinglist.txt";
  7. $showDebugInfo=FALSE; // Do you want verbose output?
  8.  
  9. // Stop editing here
  10.  
  11. // PingRPC.php
  12. //
  13. // 2007 by Sascha Tayefeh
  14. // <a href="http://www.tayefeh.de" target="_blank">http://www.tayefeh.de</a>
  15. //
  16. // This is a PHP5-based XML-RPC ping script. It reads a one-column
  17. // fully qualified URL-list from a file ($pingListFile). Here is
  18. // an example how this file must look like:
  19. // ----------------------
  20. // <a href="http://rpc.icerocket.com:10080/" target="_blank">http://rpc.icerocket.com:10080/</a>
  21. // <a href="http://rpc.pingomatic.com/" target="_blank">http://rpc.pingomatic.com/</a>
  22. // <a href="http://rpc.technorati.com/rpc/ping" target="_blank">http://rpc.technorati.com/rpc/ping</a>
  23. // <a href="http://rpc.weblogs.com/RPC2" target="_blank">http://rpc.weblogs.com/RPC2</a>
  24. // ----------------------
  25.  
  26. $replacementCount=0;
  27. $userAgent="pingrpc.php by tayefeh";
  28.  
  29. // Read pinglist file. Must contain one fully qualified URL
  30. // (e.g: <a href="http://rpc.technorati.com/rpc/ping)" target="_blank">http://rpc.technorati.com/rpc/ping)</a> PER LINE (->
  31. // delimiter is an ASCII-linebreak)
  32. $fp=fopen($pingListFile,"r");
  33. while ( ! feof( $fp) )
  34. {
  35. $line = trim(fgets( $fp, 4096));
  36. // get the hostname
  37. $host=$line; // Make a copy of $line
  38. $host=preg_replace('/^.*http:\/\//','',$host); // Delete anything before http://
  39. $host=preg_replace('/\/.*$/','',$host); // Delete anything after behind the hostname
  40.  
  41. // get the path
  42. $path=$line; // Make another copy of $line
  43. $path=preg_replace('/^.*http:\/\/[a-zA-Z0-9\-_\.]*\.[a-zA-Z]{1,3}\//','',$path,-1,$replacementCount); // Delete anything before the path
  44. if(!$replacementCount) $path=''; // if there was no replacement (i.e. no explicit path), act appropiately
  45. if($host) $myList[$host]=$path;
  46. }
  47. echo "<h1>Ping process started</h1>";
  48.  
  49. echo "<p>Reading URLs from file $pingListFile: ";
  50. echo count($myList)." urls read.</p>";
  51.  
  52.  
  53. // Use DOM to create the XML-File
  54. $xml= new DOMDocument('1.0');
  55. $xml->formatOutput=true;
  56. $xml->preserveWhiteSpace=false;
  57. $xml->substituteEntities=false;
  58.  
  59. // Create the xml structure
  60. $methodCall=$xml->appendChild($xml->createElement('methodCall'));
  61. $methodName=$methodCall->appendChild($xml->createElement('methodName'));
  62. $params=$methodCall->appendChild($xml->createElement('params'));
  63. $param[1]=$params->appendChild($xml->createElement('param'));
  64. $value[1]=$param[1]->appendChild($xml->createElement('value'));
  65. $param[2]=$params->appendChild($xml->createElement('param'));
  66. $value[2]=$param[2]->appendChild($xml->createElement('value'));
  67.  
  68. // Set the node values
  69. $methodName->nodeValue="weblogUpdates.ping";
  70. $value[1]->nodeValue=$blogTitle;
  71. $value[2]->nodeValue=$blogUrl;
  72.  
  73. $xmlrpcReq = $xml->saveXML(); // Write the document into a string
  74. $xmlrpcLength = strlen( $xmlrpcReq ); // Get the string length.
  75.  
  76. echo "Here&apos;s the xml-message I generated (size: $xmlrpcLength bytes):";
  77.  
  78. echo "\n<pre>\n";
  79. echo htmlentities($xmlrpcReq);
  80. echo "</pre>";
  81.  
  82.  
  83. echo "<dl>";
  84.  
  85. // Proceed every link read from file
  86. foreach ( $myList as $host => $path)
  87. {
  88. if($showDebugInfo) echo "<hr/>";
  89.  
  90. echo "<dt><strong>Pinging host: $host </strong>";
  91. $httpReq = "POST /" . $path . " HTTP/1.0\r\n";
  92. $httpReq .= "User-Agent: " . $userAgent. "\r\n";
  93. $httpReq .= "Host: " . $host . "\r\n";
  94. $httpReq .= "Content-Type: text/xml\r\n";
  95. $httpReq .= "Content-length: $xmlrpcLength\r\n\r\n";
  96. $httpReq .= "$xmlrpcReq\r\n";
  97. echo "</dt>";
  98.  
  99. if($showDebugInfo)
  100. {
  101. echo "<dd><strong>Request:</strong><pre><span style=\"color: #cc9900\">".htmlentities($httpReq)."</span></pre>";
  102. echo "<strong>Answer</strong>:<span style=\"color: #99cc00\"><pre>";
  103. }
  104.  
  105.  
  106. // Actually, send ping
  107. if ( $pinghandle = @fsockopen( $host, 80 ) )
  108. {
  109. @fputs( $pinghandle, $httpReq );
  110. while ( ! feof( $pinghandle ) )
  111. {
  112. $pingresponse = @fgets( $pinghandle, 128 );
  113. if($showDebugInfo) echo htmlentities($pingresponse);
  114. }
  115. @fclose( $pinghandle );
  116. }
  117. if($showDebugInfo) echo "</span></pre></dd>";
  118. }
  119. echo "</dl>";
  120. echo "<p>FINISHED</p>";
  121. }
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.