I am new to PHP - Started little over a month back with PHP, JS and HTML with a few hours a day.
I have done a lot of homework on this but can't make head and tail out when it comes to implementation. I don't have a hardcore programmer who could sit with me on this, hence here. But I am sure if this question is answered, it would help a lot of newbir and experienced programmers there
The Problem - I can understand the concept of SOAP but cant makout how to make a simple soap request. What would be the steps in PHP.
//php SOAP extention is properly installed
Create a new SOAP client :
$variable = new SoapClient ;
$inputxml = " the input XML file as a string ";
varibale__doRequest($input xml, "string location -??
I have the XML input as a variable.. what location do i specify.. need to enable cache and a cache location ??,...)
Thats the php function i am trying to use - how to use this http://www.php.net/manual/en/soapclient.dorequest.php
// PHP code to obtain response as XML
// How to store the reponse in a variable as a string
Can someone demonstrate a simple SOAP request using PHP on a blog.
I couldnt find one example, all focus on creating SOAP services or SOAP servers.. I just wanna create a SOAP client and make a request in PHP - any detailed laymnas guide - PHP specific ??
This is the SOAP service I am struggling with - Find Company by Keyword at
http://developer.dnb.com/service-directory/sales/16682663-1.html
If someone could write a single post blog explaning the details, would be immensely grateful
PS: Not sure if my frustration is justified, but SOAP and REST concepts make programming sound like Rocket Science.. I have spent over a week, 8 hrs a day, dont have a clear idea on making SOAP requests though I have a fair understanding of the SOAP concept. As far as REST goes, it has me super confused - i don wanna touch that thing here at the moment.
There is the php soap client but you dont HAVE to use it..
you can build your request manually and post it with curl, you can then parse the response as you like.
/**
* Request login body
*/
const REQUEST_LOGIN = '<?xml version="1.0" encoding="utf-8"?>
<env:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://www.company.com/soap" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding">
<env:Body>
<ns1:RequestLogin xmlns:ns1="http://www.company.com/soap">
<ns1:Name>%username%</ns1:Name>
<ns1:OrgId>0</ns1:OrgId>
<ns1:AuthType>simple</ns1:AuthType>
</ns1:RequestLogin>
</env:Body>
</env:Envelope>
';
SOAP in PHP is a little easier than it seems. If you are looking for a long form tutorial there is a nice one up on DevZone. I would also keep the SoapClient class documentation handy.
The gist of working with Soap
// $endpoint is the callback/uri of the WSDL.
// $options is an array of options.
$client = new SoapClient($endpoint, $options);
You can learn more about he options and endpoint in the details for the constructor.
PHP has some magic methods and __call is one of them. The easiest way to use SOAP is often to just call an available method. For example:
$response = $client->getLoginToken(array(
'siteid' => 'foo',
'secid' => 'bar',
));
In this case getLoginToken is a SOAP method call. The SOAP client sees that it's not a method on the object and passes that as the method on to SOAP. The array passed in is arguments to pass on to the SOAP endpoint as arguments.
I would also checkout the examples on the soapCall page. Best of luck.
Related
I’m trying to invoke a WCF service (.NET) from PHP. It’s a little more complicated than just using a SoapClient since the service uses a WS2007FederationHttpBinding to authenticate.
Here’s the code I’m using at the moment. I haven’t even added credentials as I’m not sure how, but regardless, I’m not even at the point where I’m getting access denied errors.
$wsdl = "https://slc.centershift.com/sandbox40/StoreService.svc?wsdl";
$client = new SoapClient($wsdl,array(
//'soap_version'=>SOAP_1_2 // default 1.1, but this gives 'Uncaught SoapFault exception: [HTTP] Error Fetching http headers'
));
$params = array();
$params['SiteID'] = 123;
$params['GetPromoData'] = false;
$ret = $client->GetSiteUnitData(array('GetSiteUnitData_Request'=>$params));
print_r($ret);
Which WSDL should I be pointing to?
https://slc.centershift.com/Sandbox40/StoreService.svc?wsdl
Seems to be very short, but includes a reference to (note the wsdl0) https://slc.centershift.com/Sandbox40/StoreService.svc?wsdl=wsdl0
https://slc.centershift.com/Sandbox40/StoreService.svc?singleWsdl
Seems to have everything in it.
Do I need to specify SOAP 1.2? When I do, I get a connection timeout ([HTTP] Error Fetching http headers). When I don’t, the default of SOAP 1.1 is used and I get a [HTTP] Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. Is this because I’m not authenticated yet, or because I’m using the wrong SOAP version?
How to authenticate in PHP? Here’s the corresponding .NET/C# code. Do I need to somehow put these as SOAP headers? Or am I thinking about it all wrong, and I need to do some kind of authentication before I even call the method (from what I read, I’m supposed to get a token back and then use it for all future method calls – I think I see an example of this in an answer here on Stack Overflow.
If I call $client->__getFunctions(), using either WSDL and either SOAP version, I’m getting a valid list of all functions, so I assume either of these is fine and my real issue is the authentication.
Other programmers I’ve talked to had spent time trying to get this to work, but gave up and instead implemented a proxy in .NET. They pass their parameters from PHP to their own unsecured .NET service, which in turn calls this secure service. It works, but seems crazily inefficient to me, and counter-productive, as the purpose of WCF is to support all types of clients (even non-HTTP ones!).
I’ve read How to: Create a WSFederationHttpBinding on MSDN, but it didn’t help.
You can use this URL for WSDL https://slc.centershift.com/Sandbox40/StoreService.svc?singleWsdl. This WSDL has all definitions.
You have to use 1.2 because this webservice works with SOAP 1.2 version. I tried it with 1.1 and 1.2 and both of them gived error. 1.1 is version error, 1.2 is timeout error. I think there is an error at this test server. I used it with svcutil to generate code but it gived error too. Normaly it should get information and generate the code example to call service.
Normally you can add authenticate parameters with SoapHeader or directly add to options in SoapClient consruct (if service authentication is basic authentication). I write below code according to your screenshot. But it gives timeout after long wait.
$wsdl = "https://slc.centershift.com/sandbox40/StoreService.svc?wsdl";
$client = new SoapClient($wsdl,array('trace' => 1,'soap_version' => SOAP_1_2));
$security = array(
'UserName' => array(
'UserName'=>'TestUser',
'Password'=>'TestPassword',
'SupportInteractive'=>false
)
);
$header = new SoapHeader('ChannelFactory','Credentials',$security, false);
$client->__setSoapHeaders($header);
$params = array();
$params['SiteID'] = 100000000;
$params['Channel'] = 999;
try {
$ret = $client->GetSiteUnitData($params);
print_r($ret);
}catch(Exception $e){
echo $e->getMessage();
}
__getFunctions works, because it prints functions defined in WSDL. There is no problem with getting WSDL information at first call. But real problem is communication. PHP gets WSDL, generates required SOAP request then sends to server, but server is not responding correctly. SOAP server always gives a response even if parameters or request body are not correct.
You should communicate with service provider, I think they can give clear answer to your questions.
Having worked with consuming .NET WS from PHP before I believe you would need to create objects from classes in PHP that matches the names that .NET is expecting. The WSDL should tell you the types it is expecting. I hope this assist with your path forward!
If the SOAP call works from a C# application, you could use Wireshark (with the filter ip.dst == 204.246.130.80) to view the actual request being made and then construct a similar request from php.
Check this answer to see how you can do a custom SOAP call.
There's also the option of doing raw curl requests, since it might be easier to build your xml body, but then you would have to parse the response yourself with simplexml.
OK, so I'm completely lost here! Been staring myself blind at the screen for two days and all the explanations/tutorials I can find on the internet assumes you've already worked with SOAP before.
Using SOAPui I was at least able to try out the request and see the result :) But I need to replicate this in PHP... e.g, make request in PHP using authentication credentials and then store the response in a mySQL table.
This is how the request looks like
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:DoThis>
<tem:licensId>[MYLICENSID]</tem:licensId>
<tem:licenskey>[MYLICENSKEY]</tem:licenskey>
<tem:guid>[AGUID]</tem:guid>
</tem:DoThis>
</soapenv:Body>
</soapenv:Envelope>
So I need to verify/authenticate the request (i guess)? But how do I do this with PHP?
Once I get the request to work, how will the response be presented? I would like to insert it into an existing mySQL DB. That is to say that this is my "end game"/the purpose of this. Retrieving information from external system, compare it to existing information in DB, and if new information exists, replace current or add new.
I tried
$wsdl = 'http://URLTOSERVICE/Export.svc?wsdl';
$client =
new SoapClient(
$wsdl,
array(
"tem:licensid" => "[MYLICENSID]",
"tem:licenskey" => "[MYLICENSKEY]",
"tem:guid" => "[AGUID]"
)
);
print_r($client->DoThis());
But the response is about as interesting as looking in to a blank paper for 2 days... i.e, returns nothing.
Like I said in the topic title and at the beginning. I have no experience with SOAP. So please, if some kind soul out there takes the time to answer, keep in mind that I need clear and precise explanations :)
And thank you to anyone taking the time to help me understanding this...
can you indicate the WSDL url? I would suggest you to try a WSDL to php converter to see more clearly how to call the SOAP Web service.
To set credentials as the request you indicate, you must use the Soapheader class, http://php.net/manual/fr/class.soapheader.php, and call the __setSoapheaders, http://www.php.net/manual/fr/soapclient.setsoapHeaders.php, before calling the operation.
Let me know if you need any help or a WSDL to php converter.
I'm a newbie at CI, and I want to retrieve XML data from web services WebLogic, the server that is located at: http://services.insw.go.id/web-services/nsw?operation.invoke=getListGA . I want to to get the XML response from the server. How should I do this?
I made this function on controllers (resttest.php)
public function getRest()
{
$this->rest->initialize(array('server' => 'http://services.insw.go.id'));
$lartas = $this->rest->get('web-services/nsw',array('operation.invoke' => 'getListGA'),'xml');
die(var_dump($lartas));
}
Sometimes I get an error like "array(0) { }" and if I refresh, I get all HTML view, the same as when I browse to: http://services.insw.go.id/web-services/nsw?operation.invoke=getListGA
Am I wrong, or missing some step, or do you have any suggestion about how to change this code?
It looks as if your webservice is using SOAP (simple object access protocol). This is not REST. You'll want to use PHP's built in Soap extension with the SoapClient class. This way it's easy to post a XML "request" to that page which will return xml results rather than a html view (I assume).
Check Soap the soap extension is loaded on your server
Read about the SoapClient http://php.net/manual/en/class.soapclient.php
See if that webservice offers a WSDL (web service description language) file.
Create an instance of a soap client, using the wsdl and call the function you require.
Simple example from PHP.net
$client = new SoapClient("http://localhost/code/soap.wsdl");
$something = $client->HelloWorld(array());
echo $something->HelloWorldResult;
To get a xml response, you do not need Codeigniter. Specifically it provide WSDL. At http://services.insw.go.id/web-services/nsw you can find the example as
String wsdlUrl = "http://services.insw.go.id:80/web-services/nsw?WSDL";
So the WSDL API would be http://services.insw.go.id:80/web-services/nsw?WSDL
Then you can check this page to see how to install soap for your php.
Then you can get a xml response by the following code:
$client = new SoapClient('http://services.insw.go.id:80/web-services/nsw?WSDL');
//var_dump($client->__getFunctions());
$response = $client->getListGA();
echo $response;
these code do not need Codeigniter.
Note: $client->__getFunctions() will show you all functions that the WSDL support and the parameters the functions need.
Good luck
Im have searched a lot for a code-snippet to autogenerate a SOAP request based on a WSDL in PHP.
We have a 3'rd party vendor creating a huge WSDL ( + related webservices) for us , and I would really like to autogenerate some SOAP-requests in PHP based on that WSDL.
I know of application specific tools like SOAPUI etc - however - I prefer to do the unittesting directly in the PHP-code.
Do you know of any PHP lib to autogenerate SOAP requests based on a WSDL? Or even better, do you have a some code to autogenerate a SOAP request?
use apigenerator.com to generate soap requests
What i would recommend you is to use the soap components of the zend framework.
For example you just setup a soap client using :
$client = new Zend_Soap_Client("url to wsdl file");
//Then you can do what ever request to the server.
$client->helloWorld("Parameter');
// In this example i guess helloWolrd is a server function.
and finally you can have the xml soap request calling:
$request = $client->getLastRequest();
just do: echo $request; and you will see the data.
Remember to change the mime code to xml so you can see the xml in the browser.
I'm in with PHP SoapServer working in non-wsdl mode. I have the server set up to handle the data and return a response using setClass(). I tried returning an associative array, but that translates into SOAP maps with items, keys and values. I'd like to respond with the following:
<soap:Body>
<AsyncResponseOperationResponse xmlns="http://www.sample.com/">
<AsyncResponseOperationResult>
<Succeeded>true</Succeeded>
<Comments>
The operation was a success
</Comments>
</AsyncResponseOperationResult>
</AsyncResponseOperationResponse>
</soap:Body>
The variables would be whether success is true or false, and the comments.
I've been trying to read about the 'typemap' option, but it is not very well documented, and what I've found so far has only confused me further. The resources I've found so far are the php unit tests, like this one and this one, as well as this stackoverflow question
Can anyone provide me an example that does what I'm trying to do? I think I would be alright switching to wsdl mode with autodiscover (using Zend's Soap Server), if that comes up as a solution.
Edit:Until I figure out how to do it the right way, I'm just writing out all the XML manually.
header("Content-type: text/xml");
echo "<?xml version="1.0" encoding="utf-8"?><soap:Envelope ...