Summary of communication methods between PHP programs and servers

Source: Internet
Author: User
Suppose there are 10 websites distributed across regions, their inventory needs to be synchronized, and the database does not support remote connection. To obtain the server inventory in real time, we can use the following methods: · CURL method · SOCKET method · The SOAP method in php5 provides the following examples to implement it: CURL-based client. php <? Php $ shortcode 'nde005 '; $ website suppose there are 10 websites distributed across regions, their inventory needs to be synchronized, and the database does not support remote connection.

To obtain the server inventory in real time, we can use the following methods:

· CURL method

· SOCKET mode

· SOAP method in php5

The following are examples to implement it:

CURL method

Client. php

<? Php
$ Response code = 'nde005 ';
$ Website = 'www .abc.com ';
$ Amt = 1;
$ Pwd = 123456;
$ Ch = curl_init ();
$ Curl_url = "http://ics1.server.com/index.php? Web = ". $ website.
"& Pwd =". $ pwd. "& action = check & consumer id =". $ response code.
"& Amt =". $ amt;
Curl_setopt ($ ch, CURLOPT_URL, $ curl_url );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // no direct output is returned to the variable
$ Curl_result = curl_exec ($ ch );
$ Result = explode (',', $ curl_result );
Curl_close ($ ch );
PRint_r ($ result );
?>

The server only needs to output the data in a certain format, and then the client can receive the data in this format, for example:

Echo "OK,". $ fpsecode. ",". $ fbalance; // separated by commas

SOCKET mode

The third-party class library HttpClient can be downloaded here: http://scripts.incutio.com/httpclient/

<? Php
Require_once 'class/HttpClient. php ';
$ Params = array ('web' => 'Www .abc.com ',
'Pwd' => '123 ',
'Action' => 'check ',
'Upload ID' => 'nde005 ',
'Amt '=> 1 );
$ PageContents = HttpClient: quickPost ('http: // ics.server.com/index.php', $ params );
$ Result = explode (',', $ pageContents );
Print_r ($ result );
?>

SOAP method in PHP5

Server. php

<? Php
Function getQuote ($ fpsecode ){
Global $ dbh;
$ Result = array ();
Try {
$ Query = "SELECT fprice, fcansale, fbalance, fbaltip FROM tblbalance where upper (trim (fpsecode) =: your code limit 1 ";
$ Stmt = $ dbh-> prepare ($ query );
$ Stmt-> execute (array (': response code' => strtoupper (trim ($ fpsecode ))));
$ Stmt-> bindColumn ('fprice', $ fprice );
$ Stmt-> bindColumn ('fcansale', $ fcansale );
$ Stmt-> bindColumn ('fbalance ', $ fbalance );
$ Stmt-> bindColumn ('fbaltip ', $ fbaltip );
While ($ row = $ stmt-> fetch (PDO_FETCH_BOUND )){
//
}
} Catch (PDOException $ e ){
Echo $ e-> getMessage ();
}
Return $ fprice; // you can return an array
}

$ Dsn = 'pgsql: host = 192. 168. *. * port = 5432 dbname = db user = 123456 passWord = 100 ';
Try {
$ Dbh = new PDO ($ dsn );
} Catch (PDOException $ e ){
Die ('connection failed: '. $ e-> getMessage ());
}
Ini_set ("soap. wsdl_cache_enabled", "0"); // disabling WSDL cache
$ Server = new SoapServer ("stockquote. wsdl"); // configuration file
$ Server-> addFunction ("getQuote ");
$ Server-> handle ();
?>

Stockquote. wsdl

<? Xml version = '1. 0' encoding = 'utf-8'?>
<Definitions name = 'stockquote'
TargetNamespace = 'http: // example.org/StockQuote'
Xmlns: tns = 'http://example.org/StockQuote'
Xmlns: soap = 'http: // schemas.xmlsoap.org/wsdl/soap /'
Xmlns: xsd = 'http: // www.w3.org/2001/XMLSchema'
Xmlns: soapenc = 'http: // schemas.xmlsoap.org/soap/encoding /'
Xmlns: wsdl = 'http: // schemas.xmlsoap.org/wsdl /'
Xmlns = 'http: // schemas.xmlsoap.org/wsdl/'>

<Message name = 'getquoterequest '>
<Part name = 'symbol' type = 'xsd: string'/>
</Message>
<Message name = 'getquoteresponse'>
<Part name = 'result' type = 'xsd: float'/>
</Message>

<PortType name = 'stockquoteporttype'>
<Operation name = 'getquote'>
<Input message = 'tns: getQuoteRequest '/>
<Output message = 'tns: getQuoteResponse '/>
</Operation>
</PortType>

<Binding name = 'stockquotebinding 'type = 'tns: stockquoteporttype'>
<Soap: binding style = 'rpc'
Transport = 'http: // schemas.xmlsoap.org/soap/http'/>
<Operation name = 'getquote'>
<Soap: operation soapAction = 'urn: xmethods-delayed-quotes # getQuote '/>
<Input>
<Soap: body use = 'encoded' namespace = 'urn: xmethods-delayed-quotes'
EncodingStyle = 'http: // schemas.xmlsoap.org/soap/encoding/'/>
</Input>
<Output>
<Soap: body use = 'encoded' namespace = 'urn: xmethods-delayed-quotes'
EncodingStyle = 'http: // schemas.xmlsoap.org/soap/encoding/'/>
</Output>
</Operation>
</Binding>

<Service name = 'stockquoteservice'>
<Port name = 'stockquoteport' binding = 'stockquotebinding '>
<Soap: address location = 'http: // 192.168.3.9/php5/server. php'/>
</Port>
</Service>
</Definitions>

Client. php

<? Php
$ Client = new SoapClient ("stockquote. wsdl ");
$ Result = $ client-> getQuote ("nde005 ");
Print_r ($ result );
?>

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.