How do I access Web Service via SOAP in PHP? - php

I tried something like this :
try{
$opts = array(
'http'=>array(
'user_agent' => 'PHPSoapClient'
)
);
$context = stream_context_create($opts);
$client = new SoapClient('http://83.166.204.26:7147/TEST/WS/Harmont%20Blaine_TEST/Page/WebItem?wsdl',
array('stream_context' => $context,
'cache_wsdl' => WSDL_CACHE_NONE));
));
print_r($client);
}
catch(Exception $e){
echo $e->getMessage();
}
and the result of my var_dump function is:
SOAP-ERROR: Parsing WSDL: Couldn't load from .... failed to load external entity...
how can i access the web service via soap ? thx.
This is the beginning of the xml :

I just downloaded the file from 83.166.204.26:7147/TEST/WS/Harmont%20Blaine_TEST/Page/WebItem?wsdl. I saved it on wsdl format and now i'm getting the functions
$client = new SoapClient("WebItem.wsdl", array('proxy_host' => "83.166.204.26",
'proxy_port' => 7147,
'proxy_login' => "xxxxxx",
'proxy_password' => "xxxxxxxx"));
echo "<pre>"; var_dump($client->__getFunctions()); echo "</pre>";

Firstly:
URL that you supply required Basic authentication. When using HTTP basic authentication, PHP will only send the credentials when invoking the service, not when fetching the WSDL.
You CAN get a wsdl, if basic authentication is required:
$login = 'xxx';
$password = 'xxx';
$client = new SoapClient(
'http://' . urlencode($login) . ':' . urlencode($password) . '#83.166.204.26:7147/TEST/WS/Harmont%20Blaine_TEST/Page/WebItem?wsdl',
array(
'login' => $login,
'password' => $password
)
);
Secondly :
Your return XML does not look like valid WSDL file. Read SoapClient documentation page for proper usage - first argument of new SoapClient() must be URI of WSDL file or null for non-WSDL usage. (Here is an example of WSDL file ) May be you need to create SoapClient in non-WSDL mode?

Related

WP/PHP SoapClient: The server was unable to process the request due to an internal error

Im trying to connect to a web service from php (WorkPress)whit soapClient and i'm getting this: 'The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.'
Any idea?
This is what I´m doing, the responce is a true or false.
$wsdlUrl = 'https://#####.svc?wsdl';
$client = new SoapClient($wsdlUrl);
var_dump($client->__getTypes());
echo '</br>';
$xmlr = new SimpleXMLElement('<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:ExisteMiembro>
<!--Optional:-->
<tem:carnet>######</tem:carnet>
<!--Optional:-->
<tem:key>#######</tem:key>
</tem:ExisteMiembro>
</soapenv:Body>
</soapenv:Envelope>');
$params = new stdClass();
$params->xml = $xmlr->asXML();
$result = new SimpleXMLElement($client->ExisteMiembro($params));
Solved, I used this code instead and it works,
ini_set('soap.wsdl_cache_enabled', 0);
ini_set('soap.wsdl_cache_ttl', 900);
ini_set('default_socket_timeout', 15);
try {
$opts = array(
'http' => array(
'user_agent' => 'PHPSoapClient'
)
);
$context = stream_context_create($opts);
$wsdlUrl = 'https://######.svc?wsdl';
$soapClientOptions = array(
'stream_context' => $context,
'cache_wsdl' => WSDL_CACHE_NONE
);
$client = new SoapClient($wsdlUrl, $soapClientOptions);
$checkVatParameters = array(
'carnet' => '###',
'key' => '###'
);
$result = $client->ExisteMiembro($checkVatParameters);
print_r($result);
return true;
}
catch(Exception $e) {
echo $e->getMessage();
var_dump("entro al msj de error");
}

How to create SOAP request from XML with PHP

I am trying to create SOAP request, but it gives me error: An exception occurred whilst trying to authenticate you. String reference not set to an instance of a String. Parameter name: s
My code is:
$wsdl = "https://securedwebapp.com/api/service.asmx?WSDL";
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$xml = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:kas="KashFlow">
<soapenv:Header/>
<soapenv:Body>
<kas:GetInvoice>
<kas:UserName>xxx</kas:UserName>
<kas:Password>xxx</kas:Password>
<kas:InvoiceNumber>184576</kas:InvoiceNumber>
</kas:GetInvoice>
</soapenv:Body>
</soapenv:Envelope>';
//var_dump($xml);
$args = array(new SoapVar($xml, XSD_ANYXML));
$res = $client->__soapCall('GetInvoice', $args);
var_dump($res);
echo "<hr>Last Request";
echo "<pre>", htmlspecialchars($client->__getLastRequest()), "</pre>";
Does anyone know how to solve? The request works fine if I use:
$wsdl = 'https://securedwebapp.com/api/service.asmx?WSDL';
$trace = true;
$exceptions = false;
$debug = true;
$client = new SoapClient($wsdl,
array(
'trace' => $trace,
'exceptions' => $exceptions,
'debug' => $debug,
));
$params = array(
'InvoiceNumber' =>'81217',
'UserName' => 'xxx',
'Password' => 'xxx'
);
$resp = $client->GetInvoice($params);
print_r($resp);
This is request with simple parameters, but in real situation I have complex XML request, that I do not know how to form as php object. That is why I would send it as XML as I am trying to do at first place.
Nowadays, it is strongly advised to use a WSDL to PHP generator in order to avoid encountering this sort of issues.
I recomend using the PackageGenerator project which allows to generate a PHP SDK based on the WSDL. The generated PHP SDK contains all that is necessary to construct the request, send the request and handle the response only using an OOP approach.

Connecting to web service using SOAP & PHP

I'm trying to connect to a web service using PHP's soap client which I can successfully do using Visual Studio, pressing F5 and running the page locally which works a treat.
As soon as I upload the exact same file to my apache web host, I keep getting the error: "failed to load external entity".
Here's my code with the credentials and url taken out...
Any ideas?
<?php
header("Access-Control-Allow-Origin: http://example.com");
header("Access-Control-Request-Method: GET,POST");
ini_set('display_errors', true);
ini_set("soap.wsdl_cache_enabled", "0");
error_reporting(E_ALL);
try
{
$soapclient = new SoapClient('http://example.com');
$params = array ('SystemID' => 'testID','Username' => 'test', 'Password' => 'test');
$response = $soapclient->GetEngineerList($params);
print_r($response);
}
catch(SoapFault $e)
{
print_r($e);
}
strings are not read twice and parsed in single quotes
$soapclient = new SoapClient('$url');
try
$soapclient = new SoapClient($url);
also...do you have $url = ''; anywhere?
UPDATE 1
please try using basic auth to get to your wsdl:
$login = 'bert';
$password = 'berts password';
$client = new SoapClient(
'http://' . urlencode($login) . ':' . urlencode($password) . '#www.server.com/path/to/wsdl',
array(
'login' => $login,
'password' => $password
)
);

API request from Specific IP

I'm using Ultimate Hosting package of GoDaddy. The account has a static IP and SSL installed. Now when I'm trying to use an API which needs static IP. But scripts are sending requests from random IPs. Please suggest me an way.
My Script
$soap_exception_occured = false;
$wsdl_path = 'http://vrapi.sslwireless.com/?wsdl';
$response = '';
ini_set('soap.wsdl_cache_enabled', '0'); // disabling WSDL cache
try {
$client = new SoapClient($wsdl_path);
}
catch(SoapFault $exception) {
$soap_exception_occured = true;
$response .= '\nError occoured when connecting to the SMS SOAP Server!';
$response .= '\nSoap Exception: '.$exception;
}
I'm using SOAP. Can IP binding help me ?
Assuming you are using curl of php to connect to that API, you should bind each request to your IP:
curl_setopt($ch, CURLOPT_INTERFACE, $myIP);
To bind CURL to a different outgoing network interface or a different IP address, all that is needed is to set the CURLOPT_INTERFACE to the appropriate value before executing the CURL request:
Try this and let me know what happend
$soap_exception_occured = false;
$ipandport = array(
'socket' => array(
'bindto' => 'xx.xx.xx.xx:port',
),
);
$setip = stream_context_create(ipandport);
$wsdl_path = 'http://vrapi.sslwireless.com/?wsdl';
$response = '';
ini_set('soap.wsdl_cache_enabled', '0'); // disabling WSDL cache
try {
$client = new SoapClient($wsdl_path, array('stream_context' => $setip));
}
catch(SoapFault $exception) {
$soap_exception_occured = true;
$response .= '\nError occoured when connecting to the SMS SOAP Server!';
$response .= '\nSoap Exception: '.$exception;
}
This thread will be a not complete without file_get_contents:
$opts = array(
'socket' => array(
'bindto' => 'xx.xx.xx.xx:0',
)
);
$context = stream_context_create($opts);
echo file_get_contents('http://www.example.com', false, $context);

SOAP: Invalid HTTPS client certificate path

When I run the below-given code, the error ERROR: Invalid HTTPS client certificate path is generated. I checked that the path to certificate.pem and test.wsdl is correct. What might be the reason of such error?
$wsdl = 'http://localhost:10088/test/test.wsdl';
$options = array(
'local_cert' => 'http://localhost:10088/test/certificate.pem',
'soap_version' => SOAP_1_1
);
try {
$client = new Zend_Soap_Client($wsdl, $options);
$result = $client->getLastResponse();
print_r($result);
} catch (SoapFault $s) {
die('ERROR: [' . $s->faultcode . '] ' . $s->faultstring);
} catch (Exception $e) {
die('ERROR: ' . $e->getMessage());
}
The value for the option local_cert must be a file path instead of an URL. Change the code to this for $options:
$options = array(
'local_cert' => '/path_where_cert_is_stored/certificate.pem',
'soap_version' => SOAP_1_1
);
Since the documentation for Zend_Soap_Client isn't complete, you can look at PHP: SOAP - Manual for more information, because this what Zend_Soap_Client uses under the hood. In my opinion the possible arguments are better described at PHP: SoapClient::SoapClient - Manual (especially look at the example).

Categories