PHP SOAP webservice with NuSOAP gives no result if WSDL is configured - php

I have a php soap webservice which I've created with using NuSOAP. I use the file 'test.php' to test it in the browser as 'http://www.mydowmain.com:8080/webservice/5/test.php'.
My code:
webservice.php
<?php
require_once('../lib/nusoap.php');
$server = new nusoap_server();
$server ->configureWSDL('server', 'urn:server'); //this line causes to 'no result'
$server ->wsdl->schemaTargetNamespace = 'urn:server'; //this line causes to 'no result'
$server -> register('getData');
function getData ()
{
$items = array(array("item1"),array("item2"));
return $items;
}
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server ->service($HTTP_RAW_POST_DATA);
?>
test.php
<?php
require_once('../lib/nusoap.php');
$client = new nusoap_client("http://www.mydowmain.com:8080/webservice/5/webservice.php?wsdl");
$result = $client ->call('getData');
print_r($result);
?>
Problem:
If I remove these lines
$server ->configureWSDL('server', 'urn:server');
$server ->wsdl->schemaTargetNamespace = 'urn:server';
it shows me the result fine. Otherwise I get a blank screen, get nothing. But I really need to configure the WSDL.
How can I edit the webservice.php so that the WSDL will be configured and I can get the result array on the test.php ?

To see error information about client you can add this :
$result = $client->call('getData');
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2>' . $err;
// At this point, you know the call that follows will fail
exit();
}
else
{
echo $result;
}
After that, in the server.php, maybe the register needs more information about the return value.
$server->register('getData',
array("response"=>"xsd:string"),
'http://www.mydowmain.com:8080'
);

Try changing this:
$server ->wsdl->schemaTargetNamespace = 'urn:server';
Into this:
$server ->wsdl->schemaTargetNamespace = $namespace;
and define $namespace on top of it. That did the trick for me.
This is my code of my NuSOAP webservice:
require_once("lib/nusoap.php");
$namespace = "http://localhost:8080/Testservice/service.php?wsdl";
$server = new soap_server();
$server->configureWSDL("TestService");
$server->wsdl->schemaTargetNamespace = $namespace;

Related

Getting could not connect to host error when using soap

I am using SOAP to call a web servicefrom a Linux Centos 6 server and a php client. In this week I have been getting could not connect to host error from soapCall method. My code is as below and I have not changed it at all for some months but recently it gets this error most of the time. I have read most answers to related questions here but my problem have not been solved.
$wsdl="http://x.x.x.x:x/gw/services/Service?wsdl";
//Set key as HTTP Header
$aHTTP['http']['header'] = "key:" .$key ."\r\n";
$context = stream_context_create($aHTTP);
try
{
$client = new SoapClient($wsdl,array("soap_version" => SOAP_1_2,'trace' => 1,"stream_context" => $context));
}
catch(Exception $e)
{
return "something";
}
//I make $parametrs
try
{
$res = $client->__soapCall("send",array($parametrs));
}
catch(Exception $e)
{
print_r($e->getMessage()); //Most of the time it prints could not connect to host
}
I changed SOAP from _1_1 to _1_2 but nothing changed.
This is how I call SOAP webservice, please note Service?wsdl should be Service.wsdl
Example
//Initialize values
$wsdl = "Service.wsdl";
$url = "http://x.x.x.x:x/gw/services/";
$username = "********"; //add username
$password = "********"; //add password
$client = new SoapClient("$url".$wsdl);
$params = array(
"username"=>$username,
"password"=>$password
);
$response = $client->UserLogIn($params); //UserLogIn is the function name
var_dump($response); // to see webservice response

Trouble creating web service with PHP NuSOAP

I'm trying to set up a simple web service but I'm having trouble. The service seems to be available, but I can't seem to return a response. Var_dump on $client shows a connection to the web service, but nothing comes back in response. No errors are caught either.
Any help would be greatly appreciated.
server.php
<?php
require_once ("lib/nusoap.php");
$URL = "https://www.domain.com";
$namespace = $URL . '?wsdl';
$server = new soap_server;
$server->debug_flag = false;
$server->configureWSDL('Test', $namespace);
$server->wsdl->schemaTargetNamespace = $namespace;
function get_message($your_name)
{
if(!$your_name)
{
return new soap_fault('Client','','Put Your Name!');
}
$result = "Welcome to ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP";
return $result;
}
$server->register('get_message');
// create HTTP listener
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
exit();
?>
client.php
<?php
$wsdl = "https://www.domain.com/webservice/server.php?wsdl";
require_once ('lib/nusoap.php');
$param = array("your_name" => "Liam");
$client = new SoapClient($wsdl, array("trace" => true));
$response = $client->get_message($param);
if($client->fault)
{
echo "FAULT: <p>Code: (".$client->faultcode."</p>";
echo "String: ".$client->faultstring;
}
else
{
echo $response;
}
?>
There were bugs in the code of client and server files, I have updated the code
server.php
<?php
require_once ("lib/nusoap.php");
$URL = "http://www.domian.com/server.php";
$namespace = $URL . '?wsdl';
$server = new soap_server(); //added ()
$server->debug_flag = false;
$server->configureWSDL('Test', $namespace);
//$server->wsdl->schemaTargetNamespace = $namespace;
function get_message($your_name)
{
if(!$your_name)
{
return new soap_fault('Client','','Put Your Name!');
}
$result = "Welcome to ".$your_name .". Thanks for Your First Web Service Using PHP with SOAP";
return $result;
}
$server->register('get_message',
array('request' => 'xsd:ArrayReq'), // ArrayReq is type for your parameters
array('return' => 'xsd:String'),
'urn:Test',
'urn:Test#get_message',
'rpc',
'encoded',
'show message'); // added request return array
// create HTTP listener
//$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service(file_get_contents('php://input'));
exit();
?>
Client.php
<?php
$wsdl = "http://www.domian.com/server.php?wsdl";
require_once ('lib/nusoap.php');
$param = array("your_name" => "Liam");
$client = new nusoap_client($wsdl); // user nusoap_client() for creating client object
//$client = new SoapClient($wsdl, array("trace" => true));
$response = $client->call('get_message', $param); // use call() to call the server functions
if($client->fault)
{
echo "FAULT: <p>Code: (".$client->faultcode."</p>";
echo "String: ".$client->faultstring;
}
else
{
echo $response;
}
?>
it's working!

Salesforce error: Element {}item invalid at this location

i am using the below code to connect to salesforce using php
require_once ('SforcePartnerClient.php');
require_once ('SforceHeaderOptions.php');
require_once ('SforceMetadataClient.php');
$mySforceConnection = new SforcePartnerClient();
$mySforceConnection->createConnection("cniRegistration.wsdl");
$loginResult = $mySforceConnection->login("username", "password.token");
$queryOptions = new QueryOptions(200);
try {
$sObject = new stdclass();
$sObject->Name = 'Smith';
$sObject->Phone = '510-555-5555';
$sObject->fieldsToNull = NULL;
echo "**** Creating the following:\r\n";
$createResponse = $mySforceConnection->create($sObject, 'Account');
$ids = array();
foreach ($createResponse as $createResult) {
print_r($createResult);
array_push($ids, $createResult->id);
}
} catch (Exception $e) {
echo $e->faultstring;
}
But the above code is connect to salesforce database.
But is not executing the create commands. it's giving me the below error message
Creating the following: Element {}item invalid at this location
can any one suggest me to overcome the above problem
MAK, in your sample code SessionHeader and Endpoint setup calls are missing
$mySforceConnection->setEndpoint($location);
$mySforceConnection->setSessionHeader($sessionId);
after setting up those, if you still see an issue, check the namespace urn
$mySforceConnection->getNamespace
It should match targetNamespace value in your wsdl
the value of $mySforceConnection should point to the xml file of the partner.wsdl.xml.
E.g $SoapClient = $sfdc->createConnection("soapclient/partner.wsdl.xml");
Try adding the snippet code below to reference the WSDL.
$sfdc = new SforcePartnerClient();
// create a connection using the partner wsdl
$SoapClient = $sfdc->createConnection("soapclient/partner.wsdl.xml");
$loginResult = false;
try {
// log in with username, password and security token if required
$loginResult = $sfdc->login($sfdcUsername, $sfdcPassword.$sfdcToken);
}
catch (Exception $e) {
global $errors;
$errors = $e->faultstring;
echo "Fatal Login Error <b>" . $errors . "</b>";
die;
}
// setup the SOAP client modify the headers
$parsedURL = parse_url($sfdc->getLocation());
define ("_SFDC_SERVER_", substr($parsedURL['host'],0,strpos($parsedURL['host'], '.')));
define ("_SALESFORCE_URL_", "https://test.salesforce.com");
define ("_WS_NAME_", "WebService_WDSL_Name_Here");
define ("_WS_WSDL_", "soapclient/" . _WS_NAME_ . ".wsdl");
define ("_WS_ENDPOINT_", 'https://' . _SFDC_SERVER_ . '.salesforce.com/services/wsdl/class/' . _WS_NAME_);
define ("_WS_NAMESPACE_", 'http://soap.sforce.com/schemas/class/' . _WS_NAME_);
$urlLink = '';
try {
$client = new SoapClient(_WS_WSDL_);
$sforce_header = new SoapHeader(_WS_NAMESPACE_, "SessionHeader", array("sessionId" => $sfdc->getSessionId()));
$client->__setSoapHeaders(array($sforce_header));
} catch ( Exception $e ) {
die( 'Error<br/>' . $e->__toString() );
}
Please check the link on Tech Thought for more details on the error.

Error : HTTPFault string: Unauthorized + sharepoint web service

I have sharepoint installed in my local windows server available through LAN. Now i am running a web-service in php on my local apache server like this:
<?php
//Authentication details
$authParams = array('login' => 'username', 'password' => 'password'); \
$listName = "TestList1";
$rowLimit = '150';
$wsdl = "http://www.blah.com/sharepoint/ListsWSDL.wsdl";
//Creating the SOAP client and initializing the GetListItems method parameters
$soapClient = new SoapClient($wsdl, $authParams);
$params = array('listName' => $listName, 'rowLimit' => $rowLimit);
//Calling the GetListItems Web Service
$rawXMLresponse = null;
try{
$rawXMLresponse = $soapClient->GetListItems($params)->GetListItemsResult->any;
}
catch(SoapFault $fault){
echo 'Fault code: '.$fault->faultcode;
echo 'Fault string: '.$fault->faultstring;
}
echo '<pre>' . $rawXMLresponse . '</pre>';
//Loading the XML result into parsable DOM elements
$dom = new DOMDocument();
$dom->loadXML($rawXMLresponse);
$results = $dom->getElementsByTagNameNS("#RowsetSchema", "*");
//Fetching the elements values. Specify more attributes as necessary
foreach($results as $result){
echo $result->getAttribute("ows_LinkTitle")."<br/>";
}
unset($soapClient);
?>
<body>
</body>
</html>
But control is going to catch block with error displayed as:
Fault code: HTTPFault string: Unauthorized
Why is this happening?
First check the WSDL file, the server location is in the wsdl file, right at the bottom. Please make sure this server location is correct.
Please activate "Basic authentication" (in IIS6) for this sharepoint site you are trying to access.
Have you enabled the basic authentication in IIS ?
Look at this link :
http://blogs.iis.net/nitashav/archive/2010/02/22/iis6-0-ui-vs-iis7-x-ui-series-basic-authentication.aspx
Check your credentials.
Try to change IIS Authentication Settings to Kerberos.

SOAP returning "Internal Server Error"

My SOAP application written in NuSOAP returns an http 500 (Internal Server Error) error.
It is working fine on my local machine, I only get this error in live.
How do I diagnose this error?
Server:
require_once('nusoap.php');
// Create the server instance.
$server = new soap_server;
// Register the method to expose.
// Note: with NuSOAP 0.6.3, only method name is used without WSDL.
$server->register(
'hello', // Method name
array('name' => 'xsd:string'), // Input parameters
array('return' => 'xsd:string'), // Output parameters
'uri:helloworld', // Namespace
'uri:helloworld/hello', // SOAPAction
'rpc', // Style
'encoded' // Use
);
// Define the method as a PHP function.
function hello($name) {
require_once 'classes.php';
$db = new Database();
$sql = "select * from notifications where skey = '$name'";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
//return 'Hello, ' . $row['sales'];
$ret = "<salesdat>
<customername>". $row['sales']. "</customername>
</salesdat>";
return $ret;
}
// Use the request to (try to) invoke the service.
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
Client:
// Pull in the NuSOAP code.
require_once('nusoap.php');
// Create the client instance.
$client = new soapclient('http://----my site url ---/server.php');
//$client = new soapclient('http://localhost/cb/server.php');
// Check for an error.
$err = $client->getError();
if ($err) {
// Display the error.
echo '<p><b>Constructor error: ' . $err . '</b></p>';
// At this point, you know the call that follows will fail.
}
// Call the SOAP method.
$result = $client->call(
'hello', // method name
array('name' => 'shahidkari'), // input parameters
'uri:helloworld', // namespace
'uri:helloworld/hello' // SOAPAction
);
// Strange: the following works just as well!
//$result = $client->call('hello', array('name' => 'Scott'));
// Check for a fault
if ($client->fault) {
echo '<p><b>Fault: ';
print_r($result);
echo '</b></p>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error.
echo '<p><b>Error: ' . $err . '</b></p>';
} else {
// Display the result.
print_r($result);
}
}
This may due to the php error in your server script. Switch on the error reporting. Run the server in a browser.
server.php
error_reporting(-1);
ini_set('display_errors', 'On');
require_once './src/Test.php';
$server = new SoapServer("https://xxxx/Outbound.wsdl");
$server->setClass('Test');
$server->handle();
https://xxxx/server.php // calling this in a browser will throw the error.
In my case it was due to require_once './src/Test.php'; which was not including the class.

Categories