Send XML to Soap Web service php - php

I am really new to Web Services and xml and I'am Having problems sending xml to a web server
I' am using SoapCliente Php class
$client = new SoapClient($wsdlUrl, $soapClientOptions);
I'm not having any problem sending strings
$param = ["key" => "somekey"];
var_dump($client->somemethod($parms));
But, I am having probles sending xml data as is requered on the web service documentation
#WebParam(name = "xml") byte[] xml);
On the getTypes method:
var_dump($client->__getTypes());
Is specified in this way:
base64Binary xml
So I first stored the xml file as a string variable and then i tried:
$xmlstring = "some xml";
$xmltosend = unpack('C*', $xmlstring);
var_dump($client->somemethod($xmltosend));
And:
$xmlstring = "some xml";
$xmltosend = base64_encode($xmlstring);
var_dump($client->somemethod($xmltosend));
The Web Service always send a response acording to the data so if the send was correct i will get some succes xml data. In both cases I get this error:
FILES SENT NOT COMPLY WITH THE ESTABLISHED SPECIFICATIONS: EXTENSION, CODIFICATION
The xml format that i am sending is provided as a test example by the web service's documentation so i think the xml is Ok, so I am clueless of what I am missing.
Edit
This is exactly how the params are excected on the documentation
#WebMethod
#WebResult(name = "RespuestaRecepcionComprobante")
public RespuestaSolicitud validarComprobante(#WebParam(name = "xml") byte[]
xml);
So i just sended the xml string and it worked
$param = ["xml" => $xmlstring];
var_dump($client->validarComprobante($param));
It's ok to send the data like this?
The web service read the xml and send me an Ok response
I also expect an xml response but I dont know how to catch it

Related

How to send xml file for asp.net webservice using php soap

I have to send a XML file to asp.net based webservice. I am using php soap client.
Here is the code:
$request = file_get_contents('myfile.xml');
$result = $soap->__doRequest($request, 'LOCATION_URL', 'ACTION_URL', SOAP_1_2);
AND the xml file looks like:
<ns1:header xmlns:ns1="http://something.com">
<ns1:timestamp>2014-01-01T13:20:00.000-05:00</ns1:timestamp>
But, every time its return a blank response. I didn't get any clue from any side.
Any help will be welcome.

Calling ASP.NET Web Api method with PHP

I have the following web method in my web api controller
public HttpResponseMessage PostMakeBooking(FacilityBookingRequest bookingRequest)
{
var returnStatus = HttpStatusCode.OK;
var json = new JavaScriptSerializer().Serialize(bookingRequest);
var response = Request.CreateResponse<CardholderResponse>(returnStatus, cardholderResponse);
return response;
}
When I make this call from my .NET app, my json string appears correctly when I seralize it
{"correlationId":null,"RequestId":"7ec5092a-342a-4e32-9311-10e7df3e3683","BookingId":"BK-123102","CardholderId":"123456","BookingFrom":"\/Date(1370512706448)\/","BookingUntil":"\/Date(1370523506449)\/","DeviceId":"ACU-01-R2","Action":"Add","LoginId":"tester","Password":"tester"}
However, when I made to call from my php script
public function web_request(){
$guid =self::getGUID();
$replace = array("{","}");
$guid = str_replace($replace, "", $guid);
$client = new Zend_Rest_Client("http://203.92.72.221");
$request= new myZendCommon_FacilityBookingRequest();
$request->RequestId =$guid;
$request->BookingFrom ="27/03/2013 05:30";
$request->BookingUntil ="27/03/2013 06:30";
$request->CardholderId ="E0185963";
$request->DeviceId ="ACU-B2-01-R1";
$request->BookingId ="111";
$request->Action ="Add";
$request->LoginId ="tester";
$request->correlationId ="(null)";
$request->Password ="tester";
$request = json_encode($request);
$response = $client->restPost("/ibsswebapi/api/facilitybooking",$request);
print_r($response);
exit();
The call goes to my web method, but when I serialize it using JavaScriptSerializer().Serialize(bookingRequest)
{"correlationId":null,"RequestId":null,"BookingId":null,"CardholderId":null,"BookingFrom":"\/Date(-62135596800000)\/","BookingUntil":"\/Date(-62135596800000)\/","DeviceId":null,"Action":null,"LoginId":null,"Password":null}
All the values are null.
Is something wrong with the script?
I believe Kiran is right. Not sure why some one has felt his answer is not useful. Anyways, my understanding is that you are creating a JSON string and doing a form post of the same. I guess in this case the content type is sent as application/www-form-urlencoded but request body is a JSON string. You can use Fiddler to see how the request is being sent by the PHP script. I don't have the PHP knowledge to tell you how you can post JSON but my guess is that if you just remove the JSON encoding line $request = json_encode($request);, it should be okay.
From ASP.NET Web API point of view, if the request has Content-Type: application/json header and the body has the right JSON or if the request has Content-Type:application/www-form-urlencoded header and the body has the form url encoded content like RequestId=7ec5092a-342a-4e32-9311-10e7df3e3683&BookingId=BK-123102 and so on, web API will absolutely have no problem in binding. Currently, the request is not being sent in the right format for web API to bind.
Are you sending the header Content-Type:application/json in your request?
Also add the following piece of code to catch any model state validation errors:
.
if (!ModelState.IsValid)
{
throw new HttpResponseException(
Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState));
}

CodeIgniter Web Services Client

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

Using PHP soap service with Delphi without WSDL?

I'm quite familiar with using web services in Delphi when I have a WSDL. I simply use the wizard and in a few secs I'm up and running.
I now have a challenge where I'm given a soap interface written in PHP, without any WSDL schema.
The sample given to me for PHP is:
<?php
// The xml to be sent to the webService
$reqest_string = <<<XML
<test_api>
<request>1</request>
</test_api>
XML;
// create web service client
$client = new WSClient(array("to" => "http://api.infax.co.za/edge_api.php"));
// send request to the web service
$reply = $client->request($reqest_string);
// display the responce from the webservice
$xml_str = simplexml_load_string($reply->str);
// display response on screen
echo "Came from server = ".$xml_str->response."<br>";
?>
I've tried just post-ing the xml to the url, but I get a soap error back about a function that does not exist.
Any ideas??
For very simple SOAP web services, it could be easier to follow existing examples / documentation or (if neither is good enough) record the interaction using a HTTP proxy (fiddler2), and then code the communciation using a XML library of your choice.

Send XML with SOAP

I need to send SOAP data.
<vodh:VODHeader xmlns:vodh="http://www.example.com/soap/header/">
<vodh:info>getuser</vodh:info>
<vodh:authentication>
<vodh:username>xxx</vodh:username>
<vodh:password>xxx</vodh:password>
</vodh:authentication>
</vodh:VODHeader>
<vodb:VODBody xmlns:vodb="http://www.example.com/soap/body/" version="1.0">
<vodb:idUser>65344</vodb:idUser>
<vodb:message>Lorem ipsum</vodb:message>
</vodb:VODBody>
I was using SoapClient to create an array and pass it as parameter but is well defined so?
$client = new nusoap_client($Urlserver,FALSE);
//$data_xml is the xml I mention in previous lines
$msg = $client->serializeEnvelope($data_xml);
$result=$client->send($msg, $Urlserver);
This is the correct way to send data or other method.
And soap responds with an ACK.

Categories