SoapFault exception - php

I'm using PHP Version 5.4.16
When I make a SOAP call, it gives me error:
SoapFault exception: [soap:Server] Server was unable to process request. ---> Object reference not set to an instance of an object. in /var/www/html/VAS_sms/index.php:24
Stack trace:
#0 /var/www/html/VAS_sms/index.php(24): SoapClient->__call('SendSMS', Array)
#1 /var/www/html/VAS_sms/index.php(24): SoapClient->SendSMS(Array)
#2 {main}
That's beside a Warning:
Warning: The use statement with non-compound name 'SoapClient' has no
effect in C:\wamp\www\VAS_sms\index.php on line 2
I lost my mind making it run..
Here's my code:
<?php
use SoapClient;
ini_set("soap.wsdl_cache_enabled", "0"); // disabling WSDL cache
$wsdl_path = "https://smsvas.vlserv.com/KannelSending/service.asmx?WSDL";
$client = new SoapClient($wsdl_path, array('trace' => 1));
$userName = "username";
$Password = "password";
$SMSText = "text";
$SMSLang = "e";
$SMSSender = "sender";
$SMSReceiver = "01xxxxxxxxx";
try {
echo "<pre>\n";
$result = $client->SendSMS(array(
"Username" => $userName,
"Password" => $Password,
"SMSText" => $SMSText,
"SMSLang" => $SMSLang,
"SMSSender" => $SMSSender,
"SMSReceiver" => $SMSReceiver));
function objectToArray($d){
if (is_object($d)){
$d = get_object_vars($d);
}
if (is_array($d)){
return array_map(__FUNCTION__, $d);
}
else {return $d;}
}
$response_arr = objectToArray($result);
echo "return_code= " . str_replace(";", "", $response_arr);
echo "\n</pre>";
}
catch (SoapFault $exception) {
echo $exception;
}
?>
Any suggestions?
side Q: is there any file missing I should include instead of use SoapClient; ?!
Thanks in advance
EDIT 1
in the documentation file:
<?php
use SoapClient;
$client = new SoapClient("https://smsvas.vlserv.com/KannelSending/service.asmx");
$userName = "username";
$Password = "password";
$SMSText = "text";
$SMSLang = "e";
$SMSSender = "sender";
$SMSReceiver = "01xxxxxxxxx";
$result = $client->SendSMS(array(
"Username" => $userName,
"Password" => $Password,
"SMSText" => $SMSText,
"SMSLang" => $SMSLang,
"SMSSender" => $SMSSender,
"SMSReceiver" => $SMSReceiver));
$response_arr = objectToArray($result);
echo "return_code= " . str_replace(";", "", $response_arr);
function objectToArray($d)
{
if (is_object($d))
{$d = get_object_vars($d);}
if (is_array($d))
{return array_map(__FUNCTION__, $d);}
else {return $d;}
}
?>
Note:  We’re using PHP SoapClient to call the API Web Service, so you
need to enable the libxml PHP extension.  As its response is an
object (not string) so you need to convert it to an array by using
objectToArray function.

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

Convert PHP SoapClient to Perl SOAP::Lite

I am writing a web application that works with the five9 API. I have a working php script that uses the API to print out the email of the first User. My application is in Perl and I am trying to avoid shelling out to a PHP script. I have tried to use SOAP::Lite but am having difficulties. Here is my working PHP script.
#!/usr/bin/php
<?php
$soap = null;
$wsdl = "https://api.five9.com/wsadmin/v2/AdminWebService?wsdl";
$user = "johnsmith#example.com";
$pass = "password";
$soap_options = array("login" => $user, "password" => $pass, "trace" => 1);
$soap = new SoapClient($wsdl, $soap_options);
$arryParams['userNamePattern'] = '';
$result = $soap->getUsersInfo($arryParams);
if(isset($result->return)) {
$objRecords = $result->return;
print $objRecords[0]->generalInfo->EMail;
print "\n";
}
?>
Here is what I have so far working with perl and SOAP::Lite
#!/usr/bin/env perl
use warnings;
use strict;
use MIME::Base64;
use Data::Dumper;
use SOAP::Lite +trace => [ transport => sub { print $_[0]->as_string } ];
my $wsdl_five9 = 'https://api.five9.com/wsadmin/AdminWebService?wsdl&user=johnsmith#example.com';
my $uri = 'https://api.five9.com/wsadmin/AdminWebService';
my $user = 'johnsmith#example.com';
my $password = 'password';
my $authorize = 'Basic '.encode_base64($user . ":" . $password);
my $client = SOAP::Lite->service($wsdl_five9);
$client->on_fault(
sub {
my $soap = shift;
my $res = shift;
if(ref($res) eq '') {
die($res);
} else {
die($res->faultstring);
}
return new SOAP::SOM;
}
);
print "Done\n";
If anyone has experience working with perl and SOAP any insight would be greatly appreciated.

What would be this SOAP request in php (adding an object)

I admit to being a bit confused. I need to convert the below SOAP (.net I presume) into a php call (It is to integrate with Checkmarx - https://checkmarx.atlassian.net/wiki/display/KC/Initiating+a+Session):
public void LogAdminIn()
{
CxSDKWebServiceSoapClient cxSDKProxy = new CxSDKWebServiceSoapClient();
CxWSResponseLoginData loginResult = cxSDKProxy.Login(new Credentials() { User = "admin#cx", Pass = "admin" }, 1033);
sessionID = loginResult.SessionId;
}
This was my attempt:
$client = new SoapClient($ServiceURL);
$param = array(
'User' => $login,
'Pass' => $password,
'lcid' => "1033"
);
$result = $client->Login(new SoapParam ($param, "Credentials"));
var_dump($result);
But I have no confidence it is actually right (it doesn't work, so I suppose it isn't).
I am presuming the structure, but confused as to what it should be.
Do you have enabled soap in the php.ini file ?
extension=php_soap.dll remove the ;
and add in your script :
ini_set('display_errors', 1);
error_reporting(E_ALL);
ini_set('soap.wsdl_cache_enabled', '0');
ini_set('soap.wsdl_cache_ttl', '0');
And you need some try catch as well :
try {
$client = new SoapClient("some.wsdl");
$result = $client->SomeFunction(/* ... */);
} catch (SoapFault $fault) {
trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
}
Check that and maybe you will have some hints :)
It looks like the Login() method takes two parameters: a Credentials structure and an integer. You just need to change things slightly:
$credentials = array('User' => $login, 'Pass' => $password);
$lcid = 1033;
$result = $client->Login($credentials, $lcid);
If that doesn't work, you could also try making $credentials an object:
$credentials = new stdClass();
$credentials->User = $login;
$credentials->Pass = $password;
$lcid = 1033;
$result = $client->Login($credentials, $lcid);
After making the call, it is helpful to inspect the actual call for the XML and make sure the request is proper:
var_dump($client->__getLastRequest());

CRM dynamic "an error occurred when verifying security for the message"

I am tring to access http://xxxxxxxxxxx/CRM2011/XRMServices/2011/Organization.svc?wsdl
I can able to see functions list. means i can access to webservice. But while i tring to write data using function create it through "an error occurred when verifying security for the message" my code is below
<?php
date_default_timezone_set("Asia/Kolkata");
ini_set("soap.wsdl_cache_enabled", "0");
$location = "http://182.18.175.29/CRM2011/XRMServices/2011/Organization.svc?wsdl";
$config['Username'] = 'xxxxxxx';
$config['Password'] = 'xxxxxx';
$config['soap_version'] = SOAP_1_2;
$config['trace'] = 1; // enable trace to view what is happening
$config['use'] = SOAP_LITERAL;
$config['style'] = SOAP_DOCUMENT;
$config['exceptions'] = 0; // disable exceptions
$config["cache_wsdl"] = WSDL_CACHE_NONE; // disable any caching on the wsdl, encase you alter the wsdl server
$config["features"] = SOAP_SINGLE_ELEMENT_ARRAYS;
include_once 'ntlmSoap.php';
$client = new NTLMSoapClient($location, $config);
print('<pre>');
print_r($client->__getFunctions());
$HeaderSecurity = array("UsernameToken" => array("Username" => 'xxxxxx', "Password" => 'xxxxxx'));
$header[] = new SoapHeader($location, "Security", $HeaderSecurity);
$client->__setSoapHeaders($header);
$params = array(
"bmw_firstname" => "test",
"bmw_lastname" => "test"
);
try {
$response = $client->__soapCall("Create", $params);
var_dump($response);
} catch (Exception $e) {
print_r($e);
}
// display what was sent to the server (the request)
echo "<p>Request :" . htmlspecialchars($client->__getLastRequest()) . "</p>";
// display the response from the server
echo "<p>Response:" . htmlspecialchars($client->__getLastResponse()) . "</p>";

Add comment to issue using php and JIRA SOAP API

How do I add a comment to a JIRA Issue using PHP and Jura's SOAP API? I have the connection and tested it retrieving an existing issue, all runs good, but when I try the addComment method it returns this:
Fatal error: Uncaught SoapFault exception: [soapenv:Server.userException] org.xml.sax.SAXException: Bad types (class java.util.HashMap -> class com.atlassian.jira.rpc.soap.beans.RemoteComment) in /home/a7348186/public_html/jira.php:46 Stack trace:
#0 /home/a7348186/public_html/jira.php(46): SoapClient->__call('addComment', Array)
#1 /home/a7348186/public_html/jira.php(46):
SoapClient->addComment('16VGN3ohoo', 'NTP->29', Array)
#2 {main} thrown in /home/a7348186/public_html/jira.php on line 46
This is my code:
<?php
$emailplain = $_REQUEST['plain'];
$emailsubject = $_REQUEST['subject'];
$inputkey = $_REQUEST['key'];
$inputsumm = $_REQUEST['summ'];
$client = new SoapClient(NULL,
array(
"location" => "https://server.com/rpc/soap/jirasoapservice-v2?wsdl",
"uri" => "urn:xmethods-delayed-quotes",
"style" => SOAP_RPC,
"use" => SOAP_ENCODED
));
$token = $client->login("user", "pass");
$issueId = $inputkey;
$issue = $client->getIssue($token, $issueId);
echo("assignee:".$issue->assignee);
echo(" created:".$issue->created);
echo(" summary:".$issue->summary);
echo(" issueid:".$issue->key);
print $inputsumm;
$stringsummary = $issue->summary;
$string = $issue->summary;
$emailsubjectregexp = preg_replace('/[a-zA-Z]+/', '', $inputsumm);
$stringsumm = ('summary ~ "' . $emailsubjectregexp . '"');
$jqlstring = $stringsumm;
$searchjql = $client->getIssuesFromJqlSearch($token, $jqlstring, 100);
function printArray ($array, $devolver = false) {
$stringa = '<pre>' . print_r($array, true) . '</pre>';
if ($devolver) return $stringa;
else echo $stringa;
}
printArray($searchjql);
print_r ($searchjql);
$key = $searchjql[0]->key;
echo $key;
$client->addComment($token, $key, array('body' => 'your comment'));
?>
If you notice, the last line contains the code to execute what I want, but with no luck. Any ideas folks?
Change the way you create the Soap client object, change :
$client = new SoapClient(NULL,
array(
"location" => "https://server.com/rpc/soap/jirasoapservice-v2?wsdl",
"uri" => "urn:xmethods-delayed-quotes",
"style" => SOAP_RPC,
"use" => SOAP_ENCODED
));
to:
$client = new SoapClient("https://jira.watchitoo.com/rpc/soap/jirasoapservice-v2?wsdl");
This should work.
Full add comment code:
$issueKey = "key-123";
$username= "JiraUser";
$password= "JiraPassword";
$jiraAddress = "https://your.jira.com/rpc/soap/jirasoapservice-v2?wsdl";
$myComment = "your comment";
$soapClient = new SoapClient($jiraAddress);
$token = $soapClient->login($username, $password);
$soapClient->addComment($token, $issueKey, array('body' => $myComment));

Categories