The two most commonly used methods of Php-webservice

Source: Internet
Author: User
Tags array functions header pings split strlen
Ping Service, blog program provides a notification mechanism for the first time to post updates to the blog to provide ping service services site, write the aggregation of the time studied

Ping Service, blog program provides a notification mechanism for the first time to post updates to the blog to provide ping service services site, write the aggregation of the time studied

Look at the standard first.

This is a standard ping Service, with XMLRPC to pass the data, comments written in such detail, the code instructions do not need it, PHP5 open Xmlrpc method

client.php

<?php
$host = ' ZXSV ';
$port = 80;
$rpc _server = '/test/xmlrpc_server.php ';
$title = ' ZXSV ';
$server = ' http://zxsv/test/';
$rss = ' http://zxsv/test/rss.php ';
Weblogupdates.ping method
$Ping = xmlrpc_encode_request (' weblogupdates.ping ', Array ($title, $server));
Weblogupdates.extendedping method
$extendedPing = xmlrpc_encode_request (' weblogupdates.extendedping ', Array ($title, $server, $rss));
Call the Rpc_client_call function to send all requests to the XML-RPC server to get information
$response = Rpc_client_call ($host, $port, $rpc _server, $Ping);
$split = ' <?xml version= ' 1.0 ' encoding= ' iso-8859-1 '?> ';
$xml = Explode ($split, $response);
$xml = $split. Array_pop ($xml);
$response = Xmlrpc_decode ($xml);
Output information obtained from the RPC server side
Print_r ($response);
/**
* Functions: Functions provided to clients to connect XML-RPC server-side
Parameters
* $host hosts that need to be connected
* $port Port connecting the host
* $RPC _server XML-RPC server-side files
* $request encapsulated XML Request information
* Return: The connection successfully returned the XML information returned by the server side and failed to return false
*/
function Rpc_client_call ($host, $port, $rpc _server, $request) {
$fp = Fsockopen ($host, $port);
$query = "POST $rpc _server http/1.0\nuser_agent:xml-rpc client\nhost:". $host. " \ncontent-type:text/xml\ncontent-length: ". strlen ($request)." \ n ". $request." \ n ";
if (!fputs ($fp, $query, strlen ($query)) {
$ERRSTR = "Write error";
return false;
}
$contents = ';
while (!feof ($fp)) {
$contents. = Fgets ($FP);
}
Fclose ($FP);
return $contents;
}
?>

server.php

<?php
/**
* Functions: Functions supplied to RPC client calls
Parameters
* $method functions that the client needs to call
* $params parameter array of functions that the client needs to call
* Returns: Returns the specified call result
*/
function rpc_server_extendedping ($method, $params) {
$title = $params [0];
$server = $params [1];
$rss = $params [2];
The middle of the judgment, succeeded in returning $xml_rpc_string
$XML _rpc_string = Array (' Flerror ' =>false, ' message ' => ' for the ping. ');
return $XML _rpc_string;
}
function rpc_server_ping ($method, $params) {
$title = $params [0];
$server = $params [1];
The middle of the judgment, succeeded in returning $xml_rpc_string
$XML _rpc_string = Array (' Flerror ' =>false, ' message ' => ' for the ping. ');
return $XML _rpc_string;
}
Generate an XML-RPC server side
$xmlrpc _server = Xmlrpc_server_create ();
Registers a server-side called method Rpc_server, which actually points to the rpc_server_extendedping function
Xmlrpc_server_register_method ($xmlrpc _server, "weblogupdates.extendedping", "rpc_server_extendedping");
Xmlrpc_server_register_method ($xmlrpc _server, "weblogupdates.ping", "rpc_server_ping");
Accept the XML data that the client post.
$request = $HTTP _raw_post_data;
Print_r ($request);
Gets execution results after executing an XML request that invokes the client
$xmlrpc _response = Xmlrpc_server_call_method ($xmlrpc _server, $request, NULL);
Output the result XML after the function is processed
Header (' Content-type:text/xml ');
echo $xmlrpc _response;
Destroying XML-RPC server-side resources
Xmlrpc_server_destroy ($xmlrpc _server);
?>

The class is written with a bug

<?php
Class Pings {
Public $xmlrpc _server;
Public $xmlrpc _response;
Public $methodName;
Public Function __construct () {
Generate an XML-RPC server side
$this->xmlrpc_server = Xmlrpc_server_create ();
$this->run ();
}

Registers a server-side called method Rpc_server, which actually points to the ping function
Public Function Rpc_server () {
$this->methodname = $this->methodname? ' weblogupdates.extendedping ': ' weblogupdates.ping ';
Xmlrpc_server_register_method ($this->xmlrpc_server, $this->methodname, Array (__class__, "ping"));
}
/**
* Functions: Functions supplied to RPC client calls
Parameters
* $method functions that the client needs to call
* $params parameter array of functions that the client needs to call
* Returns: Returns the specified call result
*/
Public Function Ping ($method, $params) {
$this->title = $params [0];
$this->server = $params [1];
$this->rss = $params [2];
$this->tag = $params [3];
$a = $this->title? $this->update (): ';
$string = Array (' Flerror ' => false, ' message ' => ' to the ping. ', ' legal ' => ' you agree that use of the B Lueidea.com Ping Service is governed by the Terms's use found at www.blueidea.com. ");
return $string;
}

Public Function Update () {
Echo ' Here put some of the updated conditions ';
}

Public Function run () {
$this->rpc_server ();
$request = Isset ($GLOBALS ["Http_raw_post_data"])? File_get_contents ("Php://input"): $GLOBALS ["Http_raw_post_data"];
$this->xmlrpc_response = Xmlrpc_server_call_method ($this->xmlrpc_server, $request, NULL);
Output the result XML after the function is processed
Header (' Content-type:text/xml ');
Echo $this->xmlrpc_response;
}

Destroying XML-RPC server-side resources
Public Function __destruct () {
Xmlrpc_server_destroy ($this->xmlrpc_server);
}
}
$OBJ = new pings ();
?>

The two most common methods of WebService are written.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.