PHP, MYSQL Web Service with nusoap - php

I'm starting to learn about web services and I'm doing some tests.
I'm trying to write a service to pass all the data of a table into my client but i can't make it work.
The first one 'servei' works, but the second doesn't.
Any suggestions will be appreciated. Thanks
service.php
require 'lib/nusoap.php';
$server = new nusoap_server();
$server->configureWSDL("test" . "urn:test");
include 'functions.php';
$server->wsdl->addComplexType('servei', 'complexType', 'struct', 'all', '', array(
'preu_antic' => array('name' => 'preu_antic', 'type' => 'xsd:float'),
'preu_actual' => array('name' => 'preu_actual', 'type' => 'xsd:float'),
'descompte_servei' => array('name' => 'descompte_servei', 'type' => 'xsd:float'),
));
$server->wsdl->addComplexType('ServiceTypes', 'complexType', 'struct', 'all', '', array(
'id_tipus_servei' => array('name' => 'id_tipus_servei', 'type' => 'xsd:inter'),
'nom_tipus_servei' => array('name' => 'nom_tipus_servei', 'type' => 'xsd:string'),
'descripcio_tipus_servei' => array('name' => 'descripcio_tipus_servei', 'type' => 'xsd:string'),
));
$server->wsdl->addComplexType('ArrayOfServiceTypes', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:ServiceTypes[]')), 'tns:ServiceTypes');
$server->register('servei', array("id_servei" => 'xsd:inter'), array("return" => 'tns:servei'));
$server->register('getServiceTypes', array("idioma" => 'xsd:string'), array("return" => 'tns:ArrayOfServiceTypes'));
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
functions.php
function servei($id){
conexio();
$query_servei = mysql_query("SELECT * FROM servei WHERE id_servei = '$id'")or die(mysql_error());
$row = mysql_fetch_array($query_servei);
$preu_antic = $row['preu_antic'];
$preu_actual = $row['preu_actual'];
$descompte_servei = $row['descompte_servei'];
$servei = array('preu_antic'=>$preu_antic,'preu_actual'=>$preu_actual,'descompte_servei'=>$descompte_servei);
return $servei;
}
function getServiceTypes($idioma){
conexio();
$query = mysql_query("SELECT id_tipus_servei, nom_tipus_servei, descripcio_tipus_servei FROM idioma_tipus_servei WHERE id_idioma = '$idioma'") or die(mysql_error());
$n = 0;
while ($row = mysql_fetch_array($query)) {
$result[$n]['id_tipus_servei'] = $row['id_tipus_servei'];
$result[$n]['nom_tipus_servei'] = $row['nom_tipus_servei'];
$result[$n]['descripcio_tipus_servei'] = $row['descripcio_tipus_servei'];
$n++;
}
return $result;
}
?>
client.php
<?php
require 'lib/nusoap.php';
include 'functions.php';
$client = new nusoap_client("http://192.168.8.155:8090/webservice/service.php?wsdl");
//$id=1;
// $servei = $client -> call('servei',array("id_servei"=>"$id"));
// print_r($servei);
$idioma ='ca';
$servicetypes = $client -> call('getServiceTypes',array("idioma"=>"$idioma"));
print_r($servicetypes);
?>

OK, I handled it.
If you are using PHP 5.4 you have to comment line 6132 in nusoap.php.

Couldn't this be fixed if we were to change it from:
$this->debug("serializing array element: $k, $v of type: $typeDef[arrayType]");
To:
$this->debug("serializing array element: $k, $v of type: " . $typeDef['arrayType'] );

Related

authentication to MQ via mqseries library php

I need some help with php mqseries library.
I have some troubles connecting to Queue-manager. It does connect without authentication, but when I'm trying to use MQCSP, I get
2035 error.
I've contacted the developers, one of them told me that he no longer works on it, others don't respond.
It looks like on IBM it doesn't work.
Here is my connection code:
$cfg = array();
$cfg['ESB_ADDRESS'] = '10.4.116.110(1416)';
$cfg['ESB_CHANNEL'] = 'SITEEXT.SVRCONN';
$cfg['ESB_QUEUE_MANAGER'] = 'IIB.ADP.MI1';
$cfg['ESB_QUEUE_NAME'] = 'SITEEXT_TO_ESB';
$cfg['ESB_TOPIC_STRING'] = '';
$cfg['USERID'] = 'svcgo-site';
$cfg['PASSWORD'] = 'site91';
$cfg['QMgrName'] = 'IIB.ADP.MI1';
$cfg['DiscInterval'] = '10';
/*
$cfg['ESB_ADDRESS'] = '10.4.111.139(1414)';
$cfg['ESB_CHANNEL'] = 'SITEEXT.SVRCONN';
$cfg['ESB_QUEUE_MANAGER'] = 'QM01';
$cfg['ESB_QUEUE_NAME'] = 'TEST_QUEUE1';
//$cfg['ESB_QUEUE_NAME'] = 'SITEEXT_TO_ESB';
$cfg['ESB_TOPIC_STRING'] = '';
$cfg['USERID'] = 'svcgo-site';
$cfg['PASSWORD'] = 'site91';
//$cfg['QMgrName'] = 'QM01';
$cfg['DiscInterval'] = '10';
*/
$connectionOptions = array(
'StrucId' => MQSERIES_MQCNO_STRUC_ID,
'Version' => MQSERIES_MQCNO_CURRENT_VERSION,
'Options' => MQSERIES_MQCNO_STANDARD_BINDING,
//MQSERIES_USE_MQCSP_AUTHENTICATION,
'MQCD' => array(
//'Version' => MQSERIES_MQCD_VERSION_11,
'ChannelName' => $cfg['ESB_CHANNEL'],
'TransportType' => MQSERIES_MQXPT_TCP,
'ConnectionName' => $cfg['ESB_ADDRESS']
),
MQSERIES_USE_MQCSP_AUTHENTICATION => true,
MQSERIES_MQCSP_AUTHENTICATION => true,
USER_AUTHENTICATION_MQCSP => true,
USE_MQCSP_AUTHENTICATION => true,
MQSERIES_USER_AUTHENTICATION_MQCSP => true,
useMQCSPAuthentication => true,
'MQCSP' => array(
'Version' => MQSERIES_MQCSP_CURRENT_VERSION,
'StrucId' => MQSERIES_MQCSP_STRUC_ID,
'AuthenticationType' => MQSERIES_MQCSP_AUTH_USER_ID_AND_PWD,
'CSPUserIdPtr' => $cfg['USERID'],
'CSPUserIdLength' => strlen($cfg['USERID']),
'CSPPasswordLength' => strlen($cfg['PASSWORD']),
'CSPPasswordPtr' => $cfg['PASSWORD']
),
/*
'ClientConnPtr' => array(
//'Version' => MQSERIES_MQCD_VERSION_11,
'ChannelName' => $cfg['ESB_CHANNEL'],
'TransportType' => MQSERIES_MQXPT_TCP,
'ConnectionName' => $cfg['ESB_ADDRESS']
)
*/
);
mqseries_connx($cfg['ESB_QUEUE_MANAGER'], $connectionOptions, $connection, $completionCode, $reason);
if ($completionCode !== MQSERIES_MQCC_OK) {
die("Connx CompCode : {$completionCode} Reason : {$reason} Text : " . mqseries_strerror($reason));
}
else{echo "Good<br>";}
Php v.5.3.17
IBM MQ 9
Mqseries client library v 0.15.0
That PHP package does not have the code in it to handle the MQCSP structure and add it to the CNO. Looks like you would need to modify the _mqseries_set_mqcno_from_array function and how it is called.

Get error in Nusoap: XML error parsing SOAP payload

I'm new on Soap\NuSoap
I can connect to server site but from server send data back to client i can not get data in array to show client site. I have to test to get data and send data back to other database. I have to get in array.
it's show error
XML error parsing SOAP payload on line 2: Invalid document end
I can't fix it by myself. please help.
This is my client.php
include("lib/nusoap.php");
$client = new nusoap_client("http://192.168.20.3/soap_server/webservice.php?wsdl");
$client->soap_defencoding = 'utf-8';
$client->encode_utf8 = false;
$client->decode_utf8 = false;
$params = array(
'strName' => $_POST["strName"],
);
$result = $client->call("resultCustomer",$params);
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
exit();
}
print_r($result);
And this is server.php
require_once("lib/nusoap.php");
//Define our namespace
$namespace = "http://localhost/soap_server/webservice.php";
//Create a new soap server
$server = new soap_server();
//Configure our WSDL
$server->configureWSDL("getCustomer");
$server->wsdl->schemaTargetNamespace = $namespace;
$server->soap_defencoding = 'utf-8';
$server->encode_utf8 = false;
$server->decode_utf8 = false;
//Register our method and argument parameters
$varname = array(
'strName' => "xsd:string"
);
//Add ComplexType
$server->wsdl->addComplexType(
'ArrayOfString',
'complexType',
'array',
'',
'',
array(
'id_user' => array('name' => 'id_user', 'type' => 'xsd:string'),
'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),
'username' => array('name' => 'username', 'type' => 'xsd:string'),
'lastname' => array('name' => 'lastname', 'type' => 'xsd:string')
)
);
//Add ComplexType
$server->wsdl->addComplexType(
'ArrayOfString',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'tns:DataList[]')
),
'tns:DataList'
);
// Register service and method
$server->register('resultCustomer',$varname, array('return' => 'tns:ArrayOfString'));
function resultCustomer($strName)
{
$objConnect = mysql_connect("localhost","root","") or die(mysql_error());
$objDB = mysql_select_db("qpt-test");
$strSQL = "SELECT * FROM qpt_user where firstname like '%".$strName."%'";
$objQuery = mysql_query($strSQL) or die (mysql_error());
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
}
return $resultArray;
}
$POST_DATA = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';
// pass our posted data (or nothing) to the soap service
$server->service($POST_DATA);
exit();

Fault Code:SOAP-ENV:Server String:Fault in Fedex Rate Service Response

require_once('modules/FedEx/RateAvailableServicesService_v18_php/library/fedex-common.php5');
$path_to_wsdl = PATH."modules/FedEx/RateAvailableServicesService_v18_php/RateService_v18.wsdl";
ini_set("soap.wsdl_cache_enabled", "0");
$client = new SoapClient($path_to_wsdl, array('trace' => 1)); // Refer to http://us3.php.net/manual/en/ref.soap.php for more information
$request['WebAuthenticationDetail'] = array(
//'ParentCredential' => array(
//'Key' => $this->getProperty('key'),
//'Password' => $this->getProperty('password')
//),
'UserCredential' => array(
'Key' => $this->getProperty('key'),
'Password' => $this->getProperty('password')
)
);
$request['ClientDetail'] = array(
'AccountNumber' => $this->getProperty('shipaccount'),
'MeterNumber' => $this->getProperty('meter')
);
$request['TransactionDetail'] = array('CustomerTransactionId' => ' *** Rate Available Services Request using PHP ***');
$request['Version'] = array(
'ServiceId' => 'crs',
'Major' => '18',
'Intermediate' => '0',
'Minor' => '1'
);
$request['ReturnTransitAndCommit'] = true;
$request['RequestedShipment']['DropoffType'] = 'REGULAR_PICKUP'; // valid values REGULAR_PICKUP, REQUEST_COURIER, ...
$request['RequestedShipment']['ShipTimestamp'] = date('c');
// Service Type and Packaging Type are not passed in the request
$request['RequestedShipment']['ServiceType'] = 'INTERNATIONAL_PRIORITY'; // valid values STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, ...
$request['RequestedShipment']['Shipper'] = array(
'Address'=> array('StreetLines' => array($UL->str_add1,$UL->str_add2),'City' => $UL->city_name,'StateOrProvinceCode' => $UL->state_name,'PostalCode' => $UL->zipcode,'CountryCode' => $UL->country_code));
$request['RequestedShipment']['Recipient'] = array(
'Address'=>array('StreetLines' => array($this->session->get('shipping_address1'),$this->session->get('shipping_address2')),'City' => $shipping_city,'StateOrProvinceCode' => $shipping_state,'PostalCode' => $this->session->get('shipping_postal_code'),'CountryCode' => $country_code,'Residential' => false));
$request['RequestedShipment']['ShippingChargesPayment'] = array(
'PaymentType' => 'SENDER',
'Payor' => array(
'ResponsibleParty' => array(
'AccountNumber' => $this->getProperty('billaccount'),
'Contact' => null,
'Address' => array(
'CountryCode' => COUNTRY_CODE
)
)
)
);
$request['RequestedShipment']['PackageCount'] = '1';
$request['RequestedShipment']['RequestedPackageLineItems'] = array(
'0' => array(
'SequenceNumber' => 1,
'GroupPackageCount' => 1,
'Weight' => array(
'Value' => $UL->weight//,
//'Units' => 'LB'
),
'Dimensions' => array(
'Length' => $UL->length,
'Width' => $UL->width,
'Height' => $UL->height//,
//'Units' => 'IN'
)
)
);
try {
if(setEndpoint('changeEndpoint')){
$newLocation = $client->__setLocation(setEndpoint('endpoint'));
}
$response = $client ->getRates($request);
if ($response -> HighestSeverity != 'FAILURE' && $response -> HighestSeverity != 'ERROR'){
echo 'Rates for following service type(s) were returned.'. Newline. Newline;
echo '<table border="1">';
echo '<tr><td>Service Type</td><td>Amount</td><td>Delivery Date</td>';
if(is_array($response -> RateReplyDetails)){
foreach ($response -> RateReplyDetails as $rateReply){
$this->printRateReplyDetails($rateReply);
}
}else{
$this->printRateReplyDetails($response -> RateReplyDetails);
}
echo '</table>'. Newline;
printSuccess($client, $response);
}else{
printError($client, $response);
}
writeToLog($client); // Write to log file
} catch (SoapFault $exception) {
printFault($exception, $client);
}
Here is error exception which i am getting
Fault
Code:SOAP-ENV:Server
String:Fault
Request
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://fedex.com/ws/rate/v18"><SOAP-ENV:Body><ns1:RateRequest><ns1:WebAuthenticationDetail><ns1:UserCredential><ns1:Key>AndJurrJfCvvWZWn</ns1:Key><ns1:Password>Rps8Pl4jF9zGp5wiEpDRhKiHo</ns1:Password></ns1:UserCredential></ns1:WebAuthenticationDetail><ns1:ClientDetail><ns1:AccountNumber>631140688</ns1:AccountNumber><ns1:MeterNumber>107714405</ns1:MeterNumber></ns1:ClientDetail><ns1:TransactionDetail><ns1:CustomerTransactionId> *** Rate Available Services Request using PHP ***</ns1:CustomerTransactionId></ns1:TransactionDetail><ns1:Version><ns1:ServiceId>crs</ns1:ServiceId><ns1:Major>18</ns1:Major><ns1:Intermediate>0</ns1:Intermediate><ns1:Minor>1</ns1:Minor></ns1:Version><ns1:ReturnTransitAndCommit>true</ns1:ReturnTransitAndCommit><ns1:RequestedShipment><ns1:ShipTimestamp>2016-03-30T18:35:41+05:30</ns1:ShipTimestamp><ns1:DropoffType>REGULAR_PICKUP</ns1:DropoffType><ns1:ServiceType>INTERNATIONAL_PRIORITY</ns1:ServiceType><ns1:Shipper><ns1:Address><ns1:StreetLines>SUITE 5A-1204</ns1:StreetLines><ns1:StreetLines>799 E DRAGRAM</ns1:StreetLines><ns1:City>TUCSON</ns1:City><ns1:StateOrProvinceCode>AZ</ns1:StateOrProvinceCode><ns1:PostalCode>94040</ns1:PostalCode><ns1:CountryCode>US</ns1:CountryCode></ns1:Address></ns1:Shipper><ns1:Recipient><ns1:Address><ns1:StreetLines>795 E</ns1:StreetLines><ns1:StreetLines>DRAGRAM</ns1:StreetLines><ns1:City>TUCSON</ns1:City><ns1:StateOrProvinceCode>AZ</ns1:StateOrProvinceCode><ns1:PostalCode>94040</ns1:PostalCode><ns1:CountryCode>US</ns1:CountryCode><ns1:Residential>false</ns1:Residential></ns1:Address></ns1:Recipient><ns1:ShippingChargesPayment><ns1:PaymentType>SENDER</ns1:PaymentType><ns1:Payor><ns1:ResponsibleParty><ns1:AccountNumber>631140688</ns1:AccountNumber><ns1:Address><ns1:CountryCode>KWI</ns1:CountryCode></ns1:Address></ns1:ResponsibleParty></ns1:Payor></ns1:ShippingChargesPayment><ns1:PackageCount>1</ns1:PackageCount><ns1:RequestedPackageLineItems><ns1:SequenceNumber>1</ns1:SequenceNumber>
<ns1:GroupPackageCount>1</ns1:GroupPackageCount><ns1:Weight>
<ns1:Value>20</ns1:Value></ns1:Weight><ns1:Dimensions>
<ns1:Length>8</ns1:Length><ns1:Width>10</ns1:Width>
<ns1:Height>10</ns1:Height></ns1:Dimensions></ns1:RequestedPackageLineItems>
</ns1:RequestedShipment></ns1:RateRequest></SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Since you have trace on, you should look at the actual request/response data. When I took your raw POST data above (in which you shouldn't have posted your account/key info) I get back a schema validation error. Syntax looks correct but the country code for payor is KWI but should be US. I'd suggest you add some code to log the request and responses...
$soap_response_hdr = $client->__getLastResponseHeaders() ;
$soap_response = $client->__getLastResponse() ;
$soap_request_hdr = $client->__getLastRequestHeaders() ;
$soap_request = $client->__getLastRequest() ;

XML error parsing SOAP payload on line 1: Not well-formed (invalid token)

I'm trying to create a webservice with PHP and nuSoap but everytime I try to execute it I'm getting the error:
XML error parsing SOAP payload on line 1: Not well-formed (invalid token)
Can anyone see what's wrong?
service.php
<?php
require 'lib/nusoap.php';
$server = new nusoap_server();
$server->configureWSDL("casamitger" . "urn:casamitger");
$server->wsdl->schemaTargetNamespace = 'urn:casamitger';
include 'functions.php';
//getAvailabilities
$server->wsdl->addComplexType('Availabilities','complexType','struct','all','',array(
'StartDate' => array('name' => 'StartDate', 'type' => 'xsd:date'),
'EndDate' => array('name' => 'EndDate', 'type' => 'xsd:date'),
'State' => array('name' => 'State', 'type' => 'xsd:string'),
));
$server->wsdl->addComplexType('ArrayOfAvailabilities', 'complexType', 'array', '', 'SOAP-ENC:Array', array(), array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Availabilities[]')), 'tns:Availabilities');
$server->register(
'getAvailabilities',
array(
"SessionID" => 'xsd:string',
"AccommodationId" => 'xsd:integer'
),
array("return" => 'tns:ArrayOfAvailabilities')
);
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
functions.php
function getAvailabilities($sessionID, $accommodation_code) {
$connection = mysqli_connect("localhost", "root", "", "casamitger");
if (authenticate($sessionID)) {
$user = getUser($sessionID);
$query = mysqli_query($connection, "SELECT count(AccommodationId) c FROM UserAccommodations WHERE AccommodationId = '$accommodation_code' AND CompanyId = '$user'") or die();
$row = mysqli_fetch_object($query);
$count = $row->c;
if ($count > 0) {
$query = mysqli_query($connection, "SELECT StartDate,EndDate,State FROM Availabilities WHERE AccommodationId = '$accommodation_code'") or die();
$n = 0;
while ($row = mysqli_fetch_object($query)) {
$result[$n]['StartDate'] = $row->StartDate;
$result[$n]['EndDate'] = $row->EndDate;
$result[$n]['State'] = $row->State;
$n++;
}
return $result;
}
}
}
and the client.php
<?php
require 'lib/nusoap.php';
include 'functions.php';
$sessionid = '1234';
$accommodation_code = '83081';
$client = new nusoap_client("http://192.168.8.155:8090/ws/service.php?wsdl");
$availabilities = $client->call(
'getAvailabilities',
array(
"SessionID" => "$sessionid",
"AccommodationId" => "$accommodation_code",
)
);
if ($client->fault) {
echo 'Fault';
} else {
$err = $client->getError();
if ($err) {
echo $err;
} else {
print_r($servicetypes);
}
}
?>
If I call the method getAvailabilities() directly it works but it doesn't through the web service, any help will be appreciated, thanks.

How can i set attributes with NuSoap in PHP?

I am working with nuSoap to build a soap server.
But i cant get the attributes the way i want.
I would like as return value:
<return xsi:type="tns:Taxatie">
<EmailAdres OptIn="1" xsi:type="tns:string">email#domain.com</EmailAdres>
</return>
And i get:
<return xsi:type="tns:Taxatie">
<EmailAdres OptIn="1" xsi:type="tns:EmailAdres">
<EmailAdres xsi:type="xsd:string">email#domain.com</EmailAdres>
</EmailAdres>
</return>
anyone know what i must change?
Or how I should set up the arrays?
This is my test code:
<?php
require_once("nusoap.php");
$soapserver = new nusoap_server();
$soapserver->configureWSDL('thijs.test', 'urn:thijs.test');
$soapserver->wsdl->addComplexType(
'Taxatie',
'complexType',
'struct',
'all',
'',
array(
'EmailAdres' => array('name' => 'EmailAdres', 'type' => 'tns:EmailAdres')
)
);
$soapserver->wsdl->addComplexType(
'EmailAdres',
'simpleType',
'struct',
'all',
'',
array(
"EmailAdres" => array('name' => 'EmailAdres', 'type' => 'xsd:string', 'minOccurs' => 0)
),
array(
'OptIn' => array('name' => 'OptIn', 'type' => 'xsd:boolean', 'use' => 'required')
)
);
$soapserver->register('taxatie', // method name
array(), // input parameters
array('return' => 'tns:Taxatie'), // output parameters
'urn:thijs.test', // namespace
'urn:thijs.test#taxatie', // soapaction
'rpc', // style
'encoded', // use
'return something' // documentation
);
class taxatie
{
var $EmailAdres = null;
function taxatie()
{
$this->EmailAdres = new emailadres();
}
}
class emailadres
{
var $EmailAdres = 'email#domain.com';
var $OptIn = true;
}
function taxatie()
{
return new taxatie();
}
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$soapserver->service($HTTP_RAW_POST_DATA);
Thanks in advance
Why not simply return an array instead of using complex type?
e.g
$resultarray['taxatie'] = array(
'EmailAdres' => $row["EmailAdres"],
'tagcount' => $row["tagcount"],
'OptIn' => $row["OptIn"]
);

Categories