Connecting to web service using SOAP & PHP - 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
)
);

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

unable to receive xml response from SOAP web service using CRON

I have a function which handles the SOAP based web service.
When I run the function via URL I am getting XML response. but when I run the function through CRON I am not getting xml response.
<?php
$logFile = 'checking'.date('Y-m-d').'.log';
$client = new SoapClient("http://DOMAIN/services/weburl?wsdl",array( "trace" => true, 'use' => SOAP_LITERAL));
$params = array('username' => 'username', 'Password' => 'password', 'delatdate' => '17/06/2015 18:00:00');
try{
$response = $client>-productOnHand($params);
} catch(SoapFault $e){
echo $e->faultcode; echo '<br />';
echo $e->getMessage(); echo '<br />';
}
require_once 'app/Mage.php';
Mage::app();
Mage::log(print_r($response, true), null, $logFile);
Mage::log(print_r($client->__getLastResponse(), true), null, $logFile);
?>
If I run this web page via URL I am getting valid xml response. But when this page is being called through CRON I am not getting valid response.
I fixed this issue.
Before default_socket_timeout was set as 60. I raised it as 600. Now I could able to receive.

How do I access Web Service via SOAP in 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?

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.

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

Categories