I'm having authentication issues with the SOAP server I'm trying to connect to.
I need to use this address for retreiving the wsdl info
https://profitweb.afasonline.nl/profitservices/updateconnector.asmx?wsdl
When I enter this address in the browser, a username and password is asked. Filling in my username and password, the WSLD XML is shown.
So, I'm using this peace of PHP code
$wsdl = "https://profitweb.afasonline.nl/profitservices/updateconnector.asmx?wsdl";
$url = "https://profitweb.afasonline.nl/profitservices/updateconnector.asmx";
$login = 'username';
$password = 'password';
$client = new SoapClient( $wsdl, array('login' => $login, 'password' => $password));
But then I get the following error:
Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'https://profitweb.afasonline.nl/profitservices/updateconnector.asmx?wsdl' : failed to load external entity "https://profitweb.afasonline.nl/profitservices/updateconnector.asmx?wsdl" in C:\xampp\htdocs\test.nl\soap.php:8 Stack trace: #0 C:\xampp\htdocs\test.nl\soap.php(8): SoapClient->SoapClient('https://profitw...', Array) #1 {main} thrown in C:\xampp\htdocs\test.nl\soap.php on line 8
This doesn't look like a authentication error. However if I download the wsdl file manually, save it local and then use that file for creating a new SoapClient, I'm not getting any errors when initializing the SoapClient.
But if I then do a request
$client->__doRequest($request, $url, 'Execute', '1');
I'm getting this __getLastResponseHeaders
HTTP/1.1 401 Unauthorized ( The server requires authorization to fulfill the request. Access to the Web server is denied. Contact the server administrator. ) WWW-Authenticate: Negotiate WWW-Authenticate: Kerberos WWW-Authenticate: NTLM Connection: Keep-Alive Pragma: no-cache Cache-Control: no-cache Content-Type: text/html Content-Length: 3184
So that's giving me the idea that I'm running into authentication issues! Read a lot of posts already about this issue, but couldn't find the right answer jet!
EDIT
Adding this to the options
'trace' => 1, 'exceptions' => 0
Give's me indeed an authentication error.
Warning: SoapClient::SoapClient(https://profitweb.afasonline.nl/profitservices/updateconnector.asmx?wsdl): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized ( The server requires authorization to fulfill the request. Access to the Web server is denied. Cont in C:\xampp\htdocs\test.nl\soap.php on line 9
AFAS still uses NuSOAP (4.x) instead of SOAP (5.x). I'm using the noiselabs/nusoap-bundle which can be found on Github to access AFAS myself.
Basically you get these kind of errors when you connect with Afasonline Soap API in PHP
Below is an example how to connect and request
NOTE: First you need to know some concept about Afasonline API before connecting as you know Afasonline uses Nusoap with Windows NTLM Authentication so in order connect you need domain name for NTLM which is “AOL” in case Afasonline. You can read more about NTLM by searching over the internet. Further you need the following parameter also in order to connect
environmentId (it always starts with OXXXXXXX), userId, Password, connectorID.
I am going show an example for Getconnector with connectorID (SSS_werkgever_vacatures) to get all the jobs but method of authentication is same for all the connectors.
Click here to see Tutorial in Detail
<?php
require_once('lib/nusoap.php');
$wsdl = 'https://profitweb.afasonline.nl/profitservices/getconnector.asmx?wsdl';
$client = new nusoap_client($wsdl, true);
$client->setCredentials("AOL" . '\\' . "53175.Webbio", "Password!", 'ntlm');
$client->setUseCurl(true);
$client->useHTTPPersistentConnection();
$client->setCurlOption(CURLOPT_USERPWD, '53175.webbio:Password!');
$xml_array['environmentId'] = 'O12343AA';
$xml_array['userId'] = "41258.Webbio";
$xml_array['password'] = "Password";
$xml_array['logonAs'] = "";
$xml_array['connectorId'] = "SSS_werkgever_vacatures";
$xml_array['filtersXml'] = "";
$err = $client->getError();
if ($err) {
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->getDebug(), ENT_QUOTES) . '</pre>';
exit();
}
$result = $client->call('GetData', array('parameters' => $xml_array), '', '', false, true);
// var_dump($result);
header('Content-Type: application/xml');
print_r($result["GetDataResult"]);
?>
Related
I am trying to setup a SOAP request in PHP using HTTP basic Authentication. For some reason I keep getting, HTTP/1.1 401 Unauthorized error.
This is an example of the request I'm trying to create:
POST https://url/someurl/soap HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "SomeSoapAction"
User-Agent: SomeUser Client-HttpClient/3.1
Content-Length: 1503
Authorization: Basic dGsomebasicreyteryheyp0ZXN0ZXI=
Host: somehost.com
This is a snippet of my code:
ini_set('display_errors',1);
ini_set("soap.wsdl_cache_enabled", "0");
error_reporting(E_ALL);
$wsdl = "urltosomewsdlfile.wsdl";
$url = "somerl";
$username = "username";
$password = "password";
$encodedstuff = base64_encode("{$username}:{$password}");
$client = new SoapClient($wsdl, array('exceptions' => true,'trace' => true,'login' => 'somelogin', 'password' => 'somepw'));
$header = new SoapHeader($url, 'Authorization: Basic', $encodedstuff);
$client->__setSoapHeaders($header);
try {
$result = $client->SomeSoapAction();
} catch (SoapFault $soapFault) {
var_dump($soapFault);
echo "<br />Request :<br />", htmlentities($client->__getLastRequest()), "<br />";
echo "<br />Response :<br />", htmlentities($client->__getLastResponse()), "<br />";
}
If I take the username and password out of the new SoapClient section it throws up Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from...
If I put in the base64 encoded variable to the new SoapClient section instead of the username/pw it throws the same Parsing error as above:
$client = new SoapClient($wsdl, array( 'authentication' => "Basic $encodedstuff", 'exceptions' => true,'trace' => true,));
The username and password work when loading the URL directly.
Can anyone point me in the right direction? Thanks for any help.
Update 12/18/18: I've been trying this as a new method suggested, but still receive the same 401 error message:
$opts = array( 'http'=>array( 'method'=>"POST", 'header' => "Authorization: Basic " . base64_encode("{$user}:{$pw}") ) );
$context = stream_context_create($opts);
$client = new SoapClient($wsdl, array('stream_context' => $context,'exceptions' => true,'trace' => true,));
$header = new SoapHeader($url, 'Authorization: Basic', $encodedstuff);
$client->__setSoapHeaders($header);
Update 15/01/19:
I'm still having issues with this.
Using SOAPUI I can make a successful request and gain authentication. I can't see what the difference is between the SOAPUI request and my own PHP request.
I've noticed that the request header created by my PHP code alters the POST and Host endpoint URLs. The first part of the POST URL has the domain removed and the Host also has the first section of the URL removed.
SOAPUI request header (correct returns 200):
POST https://test-example.com/file/SOAP HTTP/1.1
Host: test-example.com
PHP request header (returns a 401):
POST /file/SOAP HTTP/1.1
Host: example.com
This seems to be causing the problem as it looks as if I'm pointing to the wrong endpoint. Does anyone have any advice on what might be wrong with my PHP code to break this request? Thanks.
I am a beginner in Soap Web Services. I have looked over all the questions and answers related to mine but they have different scenarios and code and they don't seem to match with my requirement.
I am using a webservice created by a third party for product listing. I want to just fetch some data so that I may know the basic workflow of how Webservices work. Here is my current code
require_once('nuSOAP/lib/nusoap.php');
$wsdl = "http://ws.anchordistributors.com/anchorwebservice.asmx?wsdl";
$client = new nusoap_client($wsdl, 'wsdl');
// Input params
$username = "mylogin_id";
$password = "mylogin_password";
// In this demo, we use json data , you can use any other data format for same
$json = '{"param1":"value1","param2":"value2"}';
$client->setCredentials($username, $password);
$error = $client->getError();
if ($error)
{
echo $error; die();
}
$action = "GetCountries"; // webservice method name
$result = array();
if (isset($action))
{
$result['response'] = $client->call($action, $json);
}
echo "<h3>Output : </h3>";
print_r ($result['response']);
echo "<h2>Request</h2>";
echo "<pre>" . htmlspecialchars($client->request, ENT_QUOTES) . "</pre>";
echo "<h2>Response</h2>";
echo "<pre>" . htmlspecialchars($client->response, ENT_QUOTES) . "</pre>";
This is what I am getting back:
Output :
Array ( [faultcode] => soap:Server [faultstring] =>
System.Web.Services.Protocols.SoapException: Server was unable to
process request. ---> System.NullReferenceException: Object reference
not set to an instance of an object. at
AnchorWebservice.AnchorWebservice.AuthenticateUser(String sFunction)
at AnchorWebservice.AnchorWebservice.GetCountries() --- End of inner
exception stack trace --- [detail] => ) Request
POST /anchorwebservice.asmx HTTP/1.0 Host: ws.anchordistributors.com
User-Agent: NuSOAP/0.9.5 (1.123) Content-Type: text/xml;
charset=ISO-8859-1 SOAPAction:
"http://tempuri.org/AnchorWebservice/AnchorWebservice/GetCountries"
Authorization: Basic ODI1NjQ3OkVJTExDODI1 Content-Length: 401
{"param1":"value1","param2":"value2"}
Response
HTTP/1.1 500 Internal Server Error. Connection: close Date: Fri, 06
Oct 2017 18:47:28 GMT Server: Microsoft-IIS/6.0 X-Powered-By: ASP.NET
X-AspNet-Version: 1.1.4322 Cache-Control: private Content-Type:
text/xml; charset=utf-8 Content-Length: 744
soap:Server
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException:
Object reference not set to an instance of an object. at
AnchorWebservice.AnchorWebservice.AuthenticateUser(String sFunction)
at AnchorWebservice.AnchorWebservice.GetCountries() --- End of
inner exception stack trace ---
I have no idea why is this creating issue on what logic. Being a beginner I don't know the sense of the issue. I just want to be able to use GetCountries method to fetch the array of countries.
I have this php code and I'm trying to get content of image.jpg from url but I need to athuraize my self to the server and I did use the fowling code:
$user = 'username';
$pass = 'password';
$context = stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$user:$pass")
)
));
$data = file_get_contents("http://mysite/User%20Photos/Profile%20Pictures/bmir_LThumb.jpg", false, $context);
My problem still getting this error:
failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized
I saw many situations like me and I tried their solution like this one but still having problem .
Any help ?
I am try to fetch from soap webservice which's url is like https://something.com/webservice.asmx
I tried by setting value of CURLOPT_SSLVERSION to 3 and CURLOPT_SSL_VERIFYPEER => FALSE but it didn't worked.
Php version : 5.5.12
Apache : 2.4.9 using wamp server 2
I tried to access https://api.authorize.net/soap/v1/Service.asmx
and my code is working for this webservice but not for another webservice
my code looks like this
$client = new nusoap_client($wsdl);
$client->soap_defencoding = 'utf-8';
$mysoapmsg ='somexml';
$response = $client->send($mysoapmsg, $soapaction);
You need to put in the details about the other web service; this is very vague. A normal web service call should look like this:
$client = new SoapClient("http://domain.com/wservices.asmx?wsdl", array('login' => "USERNAME",
'password' => "PASSWORD"));
$param = array("param_name" => $param_value);
$response = $client->__call("MethodName", array("parameters" => $param));
echo "<pre>";
print_r($response);
echo "</pre>";
OK, so before I get flooded with answers like "Use PHP's built-in SOAP extension" or "upgrade to nuSOAP 0.9.5" I just want to make it clear that these are not an option in my situation. I'm working in a hosted environment and I'm lucky they even had this legacy version of nuSOAP installed on the server.
That said, I need some help with my SOAP requests.
Here's what I have so far:
require_once('nusoap_0.7.2/nusoap.php');
ini_set("soap.wsdl_cache_enabled", "0");
date_default_timezone_set('America/Los_Angeles');
$wsdl = "https://api.bronto.com/v4?wsdl";
$ns = "https://api.bronto.com/v4";
$client = new soapclient($wsdl, array('trace'=>1, 'encoding'=>'UTF-8'));
// Login
$token = "API-TOKEN";
$sessionId = $client->call('login', array('apiToken' => $token), $ns, $ns);
if (!$sessionId) {
print "Login failed.\n";
exit;
}
print_r($sessionId);
$client->setHeaders(array($ns, 'sessionHeader', array('sessionId' => $sessionId)));
echo htmlspecialchars($client->request, ENT_QUOTES); // debug request
This is what the login call to the API returns:
Array ( [faultcode] => soap:Client [faultstring] => 107: There was an error in your soap request. Please examine the request and try again. [detail] => There was an error in your soap request. Please examine the request and try again. )
This is what the SOAP request ends up looking like:
POST /v4 HTTP/1.0 Host: api.bronto.com User-Agent: NuSOAP/0.7.2 (1.94)
Content-Type: text/xml; charset=UTF-8
SOAPAction: ""
Content-Length: 379
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns7166="https://api.bronto.com/v4">
<SOAP-ENV:Body>
<parameters/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The SOAP request appears to be sending fine but the envelope is empty. I suspect I may need to replace the arrays in the request with SoapVal objects, but the documentation on this object is difficult to get my head around.
For reference, this is the PHP SOAP code I am trying to translate to nusoap 0.7.2
ini_set("soap.wsdl_cache_enabled", "0");
date_default_timezone_set('America/New_York');
$wsdl = "https://api.bronto.com/v4?wsdl";
$url = "https://api.bronto.com/v4";
$client = new SoapClient($wsdl, array('trace' => 1, 'encoding' => 'UTF-8'));
$client->__setLocation($url);
// Login
$token = "ADD YOUR API TOKEN HERE";
$sessionId = $client->login(array("apiToken" => $token))->return;
if (!$sessionId) {
print "Login failed.\n";
exit;
}
$client->__setSoapHeaders(array(new SoapHeader("http://api.bronto.com/v4", 'sessionHeader', array('sessionId' => $sessionId))));
print "Login was successful.\n";
I had had same problem even on NuSOAP/0.9.11 and later. ;-) SOAPAction: ""
NuSOAP has several issues. E.g. it supports only wsdl-mode for me, and your NuSOAP api usage is wrong (new nusoap_client second parameter).
For using wsdl-mode you must smth like this:
$wsdlurl = 'https://api-sandbox.direct.yandex.com/v5/campaigns?wsdl'; // your wsdl url
$token = '<your token if needed>';
$locale = 'en';
// Construct NuSOAP-client
$client = new nusoap_client($wsdlurl, 'wsdl'); // second parameter must be boolean true or 'wsdl'
# Auth of NuSOAP-client
$client->authtype = 'basic'; /// but Yandex Direct needs 'bearer'
$client->decode_utf8 = 0;
$client->soap_defencoding = 'UTF-8';
// ..... $client->setCredentials and $client->setHeaders if needed
// Call soap/wsdl api method
$result = $client->call(
'get',
array('SelectionCriteria' => (object) array(), 'FieldNames' => array('Id', 'Name'))
); // string $operation (required), mixed $params (required), string $namespace (optional method namespace for non-WSDL), string $soapAction (optional SOAPAction value for non-WSDL)
// Output
echo "Request:<pre>".htmlspecialchars($client->request, ENT_QUOTES)."</pre>";
echo "Response:<p>".nl2br(htmlspecialchars($client->response, ENT_QUOTES))."</p>";
// Debug
echo '<hr><pre>'.htmlspecialchars($client->debug_str, ENT_QUOTES).'</pre>';
P.S.: If you need Yandex, fixed lib is here and samples client4yandex.