Brief summary of communication methods between PHP program and server side

Source: Internet
Author: User
Tags array explode getmessage soap socket stmt trim xmlns
Program | The server assumes that there are 10 sites, distributed around them, that their inventory is synchronized, and that the database does not support remote connections.





we have to get the number of servers in real time, there are many ways to do it, I know the following:





· Curl Way





· Socket Mode





· The soap way in PHP5





to show the following example to achieve it:





Curl Way





client.php





<?php
$psecode = ’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&pseid=" . $psecode .
"&amt=" . $amt;
curl_setopt($ch, CURLOPT_URL, $curl_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//不直接输出,返回到变量
$curl_result = curl_exec($ch);
$result = explode(’,’, $curl_result);
curl_close($ch);
print_r($result);
?>



server-side only need to output in a certain format, and then the client will receive in this format, such as:





echo "OK," . $fpsecode . "," . $fbalance ;//以逗号分隔


Socket Way





this to rely on Third-party class library httpclient, you can download here: http://scripts.incutio.com/httpclient/





<?php
require_once ’class/HttpClient.php’;
$params = array(’web’ => ’www.abc.com’,
’pwd’ => ’123456’,
’action’ => ’check’,
’pseid’ => ’NDE005’,
’amt’ => 1);
$pageContents = HttpClient::quickPost(’http://ics.server.com/index.php’, $params);
$result = explode(’,’, $pageContents);
print_r($result);
?>



PHP5 soap in the way





server.php





Function GetQuote ($fpsecode) {
Global $dbh;
$result = Array ();
try {
$query = "Select Fprice, Fcansale, Fbalance, Fbaltip from Tblbalance where Upper (Trim (fpsecode)) =:p Secode L Imit 1 ";
$stmt = $dbh->prepare ($query);
$stmt->execute Array (':p secode ' => 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 ();
The
return $fprice;//You can back an array
}

$dsn = ' pgsql:host=192.168.*.* port=5432 dbname=db user=123456 password= 123456 ';
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






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/' >
















transport= ' http://schemas.xmlsoap.org/soap/http '/>



encodingstyle= ' http:// schemas.xmlsoap.org/soap/encoding/'/>


encodingstyle= ' http://schemas.xmlsoap.org/soap/encoding/'/>

operation>









client.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.