PHP SOAP Web Service - php

I am trying to create a simple PHP webservice as I am a newbie in this track. I decided to develop it using SOAP. I am using WAMP as a server and the problem is that I am unable to run the scripts nor get the WSDL file.
Here's server.php's code:
<?php
//call library
require_once ('lib/nusoap.php');
//using soap_server to create server object
$server = new soap_server;
//register a function that works on server
$server->register('get_message');
// create the function
function get_message($your_name)
{
if(!$your_name){
return new soap_fault('Client','','Put Your Name!');
}
$result = "Hello World ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP";
return $result;
}
// create HTTP listener
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
and here's a screenshot of the run:
Here's client.php's code:
<?php
require_once ('lib/nusoap.php');
//Give it value at parameter
$param = array( 'your_name' => 'Omar');
//Create object that referer a web services
$client = new soapclient('http://localhost/WebServiceSOAP/server.php');
//Call a function at server and send parameters too
$response = $client->call('get_message',$param);
//Process result
if($client->fault)
{
echo "FAULT: <p>Code: (".$client->faultcode."</p>";
echo "String: ".$client->faultstring;
}
else
{
echo $response;
}
?>
and here's a screenshot of the run:
running client.php
plus this error keeps bugging me:
Undefined variable: HTTP_RAW_POST_DATA

can u try this below code
$client = new soapclient('http://localhost/WebServiceSOAP/server.php');
to
$client = new SoapClient(
null,
array(
'location' => 'ADD YOUR LOCATION',
'uri' => 'ADD YOUR WSDL FILE ',
'trace' => 1,
'use' => SOAP_LITERAL,
)
);

You're trying to work with undefined variable $HTTP_RAW_POST_DATA. In PHP7 this hook is removed. You can read here
Instead of that I propose to do it like this:
$server->service(file_get_contents("php://input"));

Related

soap class run correctly on localhost but don't run on server

i have a problem with soap class in php. i have write a code to send sms via a sms panel. these codes run correctly on localhost (when run codes by xampp on my pc) but this code don't work when i run them on server. the php versions are same on both of them (localhost and xampp)
<?php
$FORM="30005966371";
$USERNAME="xxxx";
$PASSWORD="12345";
$DOMAIN="0098";
//---- variables ----
$TO="0935xxxxxxx";
$TEXT="test msg";
//-------------------
ini_set("soap.wsdl_cache_enabled", "0");
$sms_client = new SoapClient('http://webservice.0098sms.com/service.asmx?wsdl',array('encoding'=>'UTF-8'));
$parameters['username'] = $USERNAME;
$parameters['password'] = $PASSWORD;
$parameters['mobileno'] = $TO;
$parameters['pnlno'] = $FORM;
$parameters['text']=$TEXT;
$parameters['isflash'] =false;
echo $sms_client->SendSMS($parameters)->SendSMSResult;
?>
when i run above codes on localhost the message sends correctly but when run this code on server the following error returns:
bimehco.ir is currently unable to handle this request.
HTTP ERROR 500
i enabled soap extension in php.ini file on server but it still dont work correctly.
The initialization of the SoapClient class should look as follows when searching for errors.
$wsdl = 'http://webservice.0098sms.com/service.asmx?wsdl';
$options = [
'cache_wsdl' => WSDL_CACHE_NONE,
'exceptions' => true,
'trace' => true,
];
try {
$client = new SoapClient($wsdl, $options);
// do your request stuff here
} catch (SoapFault $fault) {
echo $fault->getMessage();
if ($client instanceof SoapClient) {
echo $client->__getLastRequest();
echo $client->__getLastResponse();
}
}
my soap codes were true and don't have any problem. it was a problem on host.
you can use above codes for soap.
or you can use curve function instead.

Consume wadl service in php

I need to call a method from a wadl service and pass a parameter
wadl: http://domain.com/application.wadl
method: checkInfo
How can i do that?
//Wsdl - Soap: I need to do this but with a wadl service
$wsdl = new SoapClient('http://domain.com/application?wsdl');
$wsdl->__call('checkInfo',array('data'=> ''));
//or
$wsdl->checkInfo(array('data'=> ''));
Thank you!!!!
you can do this by using SOAP server :
class MyClass{
function checkInfo() {
return "Hello";
}
}
//when in non-wsdl mode the uri option must be specified
$options=array('uri'=>'http://localhost/');
//create a new SOAP server
$server = new SoapServer(NULL,$options);
//attach the API class to the SOAP Server
$server->setClass('MyClass');
//start the SOAP requests handler
$server->handle();
then use it :
<?php
/*
* PHP SOAP - How to create a SOAP Server and a SOAP Client
*/
$options = array('location' => 'http://localhost/server.php',
'uri' => 'http://localhost/');
//create an instante of the SOAPClient (the API will be available)
$api = new SoapClient(NULL, $options);
//call an API method
echo $api->checkInfo();
?>
code example from here

PHP and SOAP integration

I connected to a SOAP server from a client and am trying to send form information back.
The connection works, but I have no idea how to send data back. I have received documentation ( -> http://s000.tinyupload.com/?file_id=89258359616332514672) and am stuck at the function AddApplication
This is the PHP code I've written so far. There is no form integration yet, only dummy data.
<?
$client = new SoapClient(
'https://wstest.hrweb.be/TvBastards/TvBastards/Job.svc?singleWsdl',
array(
'soap_version' => SOAP_1_1
)
);
$params = array(
'username' => 'XXX',
'password' => 'XXX',
'environmentKey' => 'XXX',
);
//Open session
try{
$token = $client->OpenSession($params);
}catch(SoapFault $ex){
echo "<pre>";
print_r($ex->detail->ExceptionDetail);
echo "</pre>";
}
//Add Application
try{
$resp = $client->AddApplication($params, ___THE_XML_SHOULD_BE_HERE___); // I have no idea how I can implement a XML file over here, and make this part work
}catch(SoapFault $ex){
echo "<pre>";
print_r($ex->detail->ExceptionDetail);
echo "</pre>";
}
//Close session
try{
$app = $client->CloseSession($token);
}catch(SoapFault $ex){
echo "<pre>";
print_r($ex);
echo "</pre>";
}`
The error I receive now is the following:
End element 'Body' from namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. Found element 'param1' from namespace ''. Line 2, position 156.
Which is understandable as I don't provide any XML.
I receive my token so the OpenSession works perfectly. As said, I'm completely stuck at the AddApplication function. This is my first encounter with a SOAP service, so every possible bit of explanation is highly appreciated.
Fixed it, and hopefully it can help out some others. I'll try and put it into steps.
define('SIM_LOGIN', 'LOGIN NAME HERE');
define('SIM_PASSWORD', 'LOGIN PASSWORD HERE');
define('ENV_KEY', 'ENVIRONMENT KEY HERE');
/*** login parameters ***/
$params = array(
'username' => SIM_LOGIN,
'password' => SIM_PASSWORD,
'environmentKey' => ENV_KEY,
);
/*** Set up client ***/
$client = new SoapClient(
__SOAP URL HERE__,
array(
'soap_version' => SOAP_1_1
)
);
After setting up the parameters and connecting to the client, we can start calling functions within the SOAP service. Every SOAP service will be different, so function names and parameters can be different. In below example I need to open a session to retrieve a token. This token is used in all the other functions, so this function is necessary. If something fails I call the "abort()" function.
try{
$token = $client->OpenSession($params);
}catch(SoapFault $ex){
abort();
}
If the token is received I call upon the function AddApplication. This expects the token parameter and an "object" (which is basically an STDClass).
I create an stdClass with all my data:
/*** Create stdClass with requested data ***/
$std = new stdClass();
$std->Firstname = $firstname;
$std->Lastname = $lastname;
$std->Birthdate = $birthdate;
$std->Phone = $phone;
$std->Email = $email;
Be sure to check camelcasing of names or capitals, as this makes all the difference.
Now we call upon the AddApplication function with parameters "token(string)" and "application(object)".
/*** AddApplication ***/
try{
$result = $client->AddApplication(array("token" => $token, "application" => $std));
}catch(SoapFault $ex){
abort();
}
If all goes well the data is stored on the external server and you receive a "success" message. It's possible that you receive a "fail" even without going into the SoapFault. Be sure to log both "$result" and "$ex", as a SOAP service can return a "Fail" but the try-catch sees this as a well formed result.
Last thing to do is close the session (and destroy the token)
/*** CloseSession ***/
try{
$app = $client->CloseSession($token);
}catch(SoapFault $ex){
abort();
}
If any questions, don't hesitate to ask them here, I'll be glad to help as I had such problems figuring this out.

Webservice in PHP with SOAP

I'm trying to get a SOAP web service to run with PHP. But when i am running the file the page shows nothing. I tried to create a client to see if the code runs that way but the function doesn't run.
Here is my code:
webservice.php
function hello(){
echo("hi");
}
$server = new SoapServer(null, array('uri'=>'http://localhost/FutureWB/hello'));
$server->addFunction("hello");
$server->handle();
testweb.php
try{
$client = new SoapClient(null, array(
'location'=>"http://localhost/FutureWB/functions/webservice.php",
'uri' => "http://localhost/FutureWB/hello"
));
$result = $client->hello();
echo($result);
}catch(SoapFault $ex){
$ex->getMessage();
}
Sorry not a great SoapClient user but how about this,
function hello(){
return "hi";
}

cakephp webservice

I have problem in creating web-service using cakephp .
this what i do to create this web-service .
I use NuSOAP - Web Services Toolkit for PHP for this.
I create a controller called WsController and import the library on it.
class WsController extends AppController{
var $uses = array();
function info() {
$this->layout= null;
$ns="http://www.techvoicellc.com/Tutorials//";
$server = new soap_server();
$server->configureWSDL('mostafa',$ns);
$server->wsdl->schemaTargetNamespace=$ns;
$server->wsdl->addComplexType('ArrayOfstring','complexType',
'array','','SOAP-ENC:Array',array()
,array(array('ref'=>'SOAP-ENC:arrayType','wsdl:arrayType'=>'string[]')),
'xsd:string');
$server->register('sum',
array('x' => 'xsd:integer','y' => 'xsd:integer'),
array('z' => 'xsd:integer'),
$ns,
"$ns#sum",
'rpc',
'encoded',
'documentation' // documentation
);
$server->service($HTTP_RAW_POST_DATA);
}
function sum($x,$y){
$z=$x+$y;
return new soapval('return','xsd:integer',$z);
}
}
and i create the clint in controller action like this
function index() {
$wsdl = 'http://localhost/asd/ws/info?wsdl';
$client = new nusoap_client ( $wsdl, true );
$this->client = new nusoap_client($wsdl, true);
$param1 = array ('x' => 2, 'y' => 1 );
$a = $client->call ( 'sum', $param1 );
echo $a;
}
it don't do any thins although that i create this in non cake project and its work very well
hope some one tell me what is the best practise to create web-service in cake php
This is quite Easy to develop web services in CakePHP. I have done it several times. Check the below steps.
class MyWebServicesController extends AppController {
var $name = 'MyWebServices';
var $layout = "ajax";
function index() {
$server = new SoapServer(null);
$server->setObject($this);
$server->handle();
exit(0);
}
public function addNumbers($a,$b) {
return $a+$b
}
}
Now your web service is hosted at http://webroot/MyWebServices
Now you can call addNumbers like below.
$client = new SoapClient(null, array('location' => "http://webroot/MyWebServices");
$sum = $client->addNumbers(1+2);
It is best to create restful web service. CakePHP has everything built in for REST. All you have to do is enable it and create json/xml views.
Here is a link with your starting point: http://book.cakephp.org/2.0/en/development/rest.html
Is there a reason you want SOAP web service?
It will be so much harder to create and test SOAP web service
SOAP will require external libraries
It will be harder for users to use the SOAP web service

Categories