Can't access Object in my Soap Client PHP - php

I'm learning how to implement Soap Web Services for a project but I'm having a bit of an issue with it. I access the client and have a method that calls the Server to get data from the database. The data coming from the database is being returned as an Object and then encoded to JSON. When I run it i get this error
SoapFault: Class 'Cow' not found in /home/ubuntu/workspace/V1Project/web_services/soap_client.php on line 10
But I tried requiring the class thinking it would "see" the Object was enfact there. But it still doesn't seem to work.
I have another method in the database that gets the data and just puts it into an array and encodes it into JSON and works fine.
Soap_Client.php
<?php
require_once('../controller/classes/Cow.php');
$option = array('location' => 'https://name_of_direct/soap_service.php',
'uri' => 'http://localhost');
try {
$client = new SoapClient(null, $option);
echo $client->getCow(40);
} catch (SoapFault $ex){
var_dump($ex);
}
?>
And...
Soap_Service.php
<?php
require_once('../model/DBController.php');
$option = array('uri' => 'http://localhost/');
$server = new SoapServer(null, $option);
$server -> setObject(new DBController());
$server -> handle();
?>
Thank you for reading and any tips or help is hugely appreciated!

Related

PHP : how to use SOAP to access a class methods that are present on a different server

I am trying to access the methods of a class that are present on a different server on my current server , so that i can share their methods , so that dont have to write the same methods again
So i configured and installed the PHP SOAP extension on my system..
After that i created a SOAPService.php file on my server side which has the following code.
<?php
// *** The SOAP class wrapper: myClassSoapServer.php ***
require_once('bc_seller_classes/Vendor/Vendor.php');
$options = array('uri' => 'http://merchants.bookchor.local/bc_seller_classes/Vendor/');
$SOAPServer = new SoapServer(null, $options);
$SOAPServer->setClass('Vendor');
$SOAPServer->handle();
?>
Here as you can see i wanted to access the Vendor class .
And on the client side i made soap.php with the following code , so that i could access the get_module() method of the Vendor class.
<html>
<head></head>
<body>
<?php
// *** The SOAP way: using a class remotely ***
$options = array(
'location' => 'http://merchants.bookchor.local/VendorSoapService.php',
'uri' => 'http://merchants.bookchor.local/'
);
$hdl = new SoapClient(null, $options);
var_dump($hdl);
$data = $hdl->get_module();
echo "hi";
var_dump($data);
?>
</body>
</html>
But when i execute soap.php on the client side it gives the following error
Uncaught SoapFault exception: [Client] looks like we got no XML document
So where am i going wrong .
PS: the get_module() function returns an array which i would like to use here.
and Both the SOAPService.php and soap.php are placed in the root folder of the project .

PHP SOAP Web Service

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"));

Brandbank API PHP Extract Data

I am working on a project that requires access to Brandbank API, however their documentation seems pretty limited as to how to access their information.
I have been given access keys and the documentation seems to direct you to extract data via SOAP siting this link https://api.brandbank.com/svc/feed/extractdata.asmx?WSDL
Can anyone provide an example of how to access the product feed of this API?
At a minimum you will need code that looks like this:
$wsdl = "https://api.brandbank.com/svc/feed/extractdata.asmx?WSDL";
try {
$soapclient = new SoapClient($wsdl, array(
'soap_version' => SOAP_1_1,
'trace' => 1,
'exceptions' => true,
)
);
$parameters = new stdClass();
$response = $soapclient->GetUnsentProductData($parameters);
} catch (Exception $e) {
var_dump($e ->getMessage());
}
The call won't work until you finish it with your access credentials, but it provides an example of how most SOAP calls look in PHP.

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";
}

Categories