I'm looking for a way to send a SOAP request from PHP code. An online game called MovieStarPlanet, has a "library" with many SOAP requests.
Here is the link to all queries:
http://www.moviestarplanet.fr/WebService/ThirdParty/ThirdPartyService.asmx
The WSDL description:
http://www.moviestarplanet.fr/WebService/ThirdParty/ThirdPartyService.asmx?WSDL
The query is like this:
POST /WebService/ThirdParty/ThirdPartyService.asmx HTTP/1.1
Host: http://www.moviestarplanet.fr
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://moviestarplanet.com/Login"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<ThirdPartyTokenHeader xmlns="http://moviestarplanet.com/">
<ThirdPartyToken>string</ThirdPartyToken>
</ThirdPartyTokenHeader>
</soap:Header>
<soap:Body>
<Login xmlns="http://moviestarplanet.com/">
<username>string</username>
<password>string</password>
</Login>
</soap:Body>
</soap:Envelope>
And the answer to this query is:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<ThirdPartyTicketHeader xmlns="http://moviestarplanet.com/">
<Ticket>string</Ticket>
</ThirdPartyTicketHeader>
</soap:Header>
<soap:Body>
<LoginResponse xmlns="http://moviestarplanet.com/">
<LoginResult>
<ServiceResult>
<Codee>int</Codee>
<Description>string</Description>
</ServiceResult>
<ActorId>int</ActorId>
<AppToken>string</AppToken>
<UserInfo>
<FriendCount>int</FriendCount>
<MembershipTimeoutDate>dateTime</MembershipTimeoutDate>
<VipTier>int</VipTier>
<Level>int</Level>
<LockedUntil>dateTime</LockedUntil>
<LockedText>string</LockedText>
<SkinSWF>string</SkinSWF>
<LastLogin>dateTime</LastLogin>
</UserInfo>
</LoginResult>
</LoginResponse>
</soap:Body>
</soap:Envelope>
That would suggest a lot of account information when the username/password combination is correct.
I did a lot of research, but I don't know how to apply the code. Here's what I did:
$soap_request = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$soap_request .= "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n";
$soap_request .= " <soap:Header>\n";
$soap_request .= " <ThirdPartyTokenHeader xmlns=\"http://moviestarplanet.com/\">\n";
$soap_request .= " <ThirdPartyToken>8346D304-F85E-4dc1-98EB-033CBEE0217F</ThirdPartyToken>\n";
$soap_request .= " </ThirdPartyTokenHeader>\n";
$soap_request .= " </soap:Header>\n";
$soap_request .= " <soap:Body>\n";
$soap_request .= " <Login xmlns=\"http://moviestarplanet.com/\">\n";
$soap_request .= " <username>string</username>\n";
$soap_request .= " <password>string</password>\n";
$soap_request .= " </Login>\n";
$soap_request .= " </soap:Body>\n";
$soap_request .= "</soap:Envelope>";
$header = array(
"POST /WebService/ThirdParty/ThirdPartyService.asmx HTTP/1.1",
"Host: http://www.moviestarplanet.fr",
"Content-type: text/xml; charset=utf-8",
"Content-length: ".strlen($soap_request),
"SOAPAction: \"http://moviestarplanet.com/Login\"",
);
$url = "http://www.moviestarplanet.fr/WebService/ThirdParty/ThirdPartyService.asmx?WSDL";
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
if(curl_exec($soap_do) === false) {
$err = 'Curl error: ' . curl_error($soap_do);
curl_close($soap_do);
print $err;
} else {
curl_close($soap_do);
print 'Success.';
}
When I run the code, it tells me an error: "Operation timed out after 10000 milliseconds with 0 bytes received".
Anyone can help me?
You are connecting to the wsdl file in your curl call. (
$url = "http://www.moviestarplanet.fr/WebService/ThirdParty/ThirdPartyService.asmx?WSDL")
The wsdl is NOT the webservice, the wsdl is a xml file with a description of the webservice.
Insted of doing all the low level coding using curl, you could use the SoapClient class:
$client=new SoapClient('http://www.moviestarplanet.fr/WebService/ThirdParty/ThirdPartyService.asmx?WSDL');
That way a lot of the basic communication with then webservice is handle automatic.
For example, you can start by listing all services on site:
print_r($client->__getFunctions());
I have found the soltuion! :)
<?php
$xml_data = '
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<ThirdPartyTokenHeader xmlns="http://moviestarplanet.com/">
<ThirdPartyToken>8346D304-F85E-4dc1-98EB-033CBEE0217F</ThirdPartyToken>
</ThirdPartyTokenHeader>
</soap:Header>
<soap:Body>
<Login xmlns="http://moviestarplanet.com/">
<username>USER</username>
<password>PASS</password>
</Login>
</soap:Body>
</soap:Envelope>
';
$headers = array(
"POST /WebService/ThirdParty/ThirdPartyService.asmx HTTP/1.1",
"Referer: www.moviestarplanet.fr",
"User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.2; GT-I9505 Build/KOT49H)",
"Content-Type: text/xml; charset=utf-8",
"Host: www.moviestarplanet.fr",
"Content-length: ".strlen($xml_data),
"Expect: 100-continue"
);
$url = 'http://www.moviestarplanet.fr/WebService/ThirdParty/ThirdPartyService.asmx?WSDL';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$reply = curl_exec($ch);
echo($reply);
?>
Related
I need to publish PHP variable values on SOAP-based web service some how I am not able to convert PHP POST values in XML elements.
How can I get POST variable, say abc, in sName element?
My code in PHP
<?php
//session_start();
$server_name= abc;
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<createtest xmlns="http://tempuri.org/">
<sA>ADD</sA>
<sType>service</sType>
<sName>"$server_name"</sName>
</createtest>
</soap:Body>
</soap:Envelope>';
$headers = array(
"Content-Type: text/xml; charset=utf-8",
"Host: http://example.com",
"Content-length: ".strlen($xml_post_string),
"SOAPAction: http://tempuri.org/createtest"
);
url = 'http://example.com'; // WSDL web service url for request
method/function
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP
request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo $response = curl_exec($ch);
?>
You have to put quotes around abc, when you define the variable as a string:
$server_name = "abc";
And you could also put the variable in the string like this to make it easier to understand it as a variable in such a long string:
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<createtest xmlns="http://tempuri.org/">
<sA>ADD</sA>
<sType>service</sType>
<sName>'.$server_name.'</sName>
</createtest>
</soap:Body>
</soap:Envelope>';
I making SOAP calls through curl in php. But this error just pops out. I guess this comes because of the soap action which has been set properly and its not empty.
The message with To '' cannot be processed at the receiver, due to an
AddressFilter mismatch at the EndpointDispatcher. Check that the
sender and receiver's EndpointAddresses agree.
I have no clue why this is coming. Am i missing something here or using something in the wrong place.
Here is my soap request
$soap_request =
'<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:bses="http://bsestarmf.in/">
<soap:Header>
</soap:Header>
<soap:Body>
<bses:getPassword>
<!--Optional:-->
<bses:UserId>123456</bses:UserId>
<!--Optional:-->
<bses:Password>789456</bses:Password>
<!--Optional:-->
<bses:PassKey>0123478</bses:PassKey>
</bses:getPassword>
</soap:Body>
</soap:Envelope>';
$header = array(
"Content-type: application/soap+xml;charset=\"utf-8\"",
"Accept: application/soap+xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://bsestarmf.in/MFOrderEntry/getPassword",
"Content-length: ".strlen($soap_request),
);
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL,"http://bsestarmfdemo.bseindia.com/MFOrderEntry/MFOrder.svc" );
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($soap_do);
dd($result);
You need to add
<wsa:To>"http://bsestarmfdemo.bseindia.com/MFOrderEntry/MFOrder.svc"</wsa:To>
to your header xml
I try to make a SOAP request:
$soapclient = new SoapClient('http://www.moviestarplanet.fr/WebService/ThirdParty/ThirdPartyService.asmx?wsdl');
$params = array('username' => 'string', 'password' => 'string');
$response = $soapclient->Login($params);
var_dump($response);
but an error telling me:
Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing
WSDL: Couldn't load from
'http://www.moviestarplanet.fr/WebService/ThirdParty/ThirdPartyService.asmx?wsdl'
: failed to load external entity[...]
I searched for a long time on the net, but I haven't found any solution... Anyone can help me, please? Thanks!
I have found the solution !
<?php
$xml_data = '
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<ThirdPartyTokenHeader xmlns="http://moviestarplanet.com/">
<ThirdPartyToken>8346D304-F85E-4dc1-98EB-033CBEE0217F</ThirdPartyToken>
</ThirdPartyTokenHeader>
</soap:Header>
<soap:Body>
<Login xmlns="http://moviestarplanet.com/">
<username>USER</username>
<password>PASS</password>
</Login>
</soap:Body>
</soap:Envelope>
';
$headers = array(
"POST /WebService/ThirdParty/ThirdPartyService.asmx HTTP/1.1",
"Referer: www.moviestarplanet.fr",
"User-Agent: Dalvik/1.6.0 (Linux; U; Android 4.4.2; GT-I9505 Build/KOT49H)",
"Content-Type: text/xml; charset=utf-8",
"Host: www.moviestarplanet.fr",
"Content-length: ".strlen($xml_data),
"Expect: 100-continue"
);
$url = 'http://www.moviestarplanet.fr/WebService/ThirdParty/ThirdPartyService.asmx?WSDL';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$reply = curl_exec($ch);
echo($reply);
?>
I am trying to parse SOAP response to JSON. So far I have this code:
$data = '<?xml version="1.0" encoding="utf-8"?>';
$data .= '<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">';
$data .= '<soap12:Body>';
$data .= '<GetCommunities xmlns="url">';
$data .= '<APIUsername>string</APIUsername>';
$data .= '<APIPassword>string</APIPassword>';
$data .= '</GetCommunities>';
$data .= '</soap12:Body>';
$data .= '</soap12:Envelope>';
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, "url" );
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $data);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($data) ));
$result = curl_exec($soap_do);
echo $result;
This code is working and I am getting following result:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetCommunitiesResponse xmlns="url">
<GetCommunitiesResult>
<Communities>
<CommunityID>1</CommunityID>
<CommunityName> Not Specified</CommunityName>
</Communities>
<Communities>
<CommunityID>276</CommunityID>
<CommunityName>Bella Toscana</CommunityName>
</Communities>
<Communities>
<CommunityID>31</CommunityID>
<CommunityName>Crescent Lakes</CommunityName>
</Communities>
<Communities>
<CommunityID>62</CommunityID>
<CommunityName>Hillcrest Estate</CommunityName>
</Communities>
<Communities>
<CommunityID>750</CommunityID>
<CommunityName>Sunny Beach</CommunityName>
</Communities>
<Communities>
<CommunityID>124</CommunityID>
<CommunityName>Terra Verde Resort</CommunityName>
</Communities>
<Communities>
<CommunityID>744</CommunityID>
<CommunityName>The Dales at West Haven</CommunityName>
</Communities>
<Communities>
<CommunityID>158</CommunityID>
<CommunityName>Westridge</CommunityName>
</Communities>
</GetCommunitiesResult>
</GetCommunitiesResponse>
</soap:Body>
</soap:Envelope>
I need to create JSON response from the data which can be found between GetCommunitiesResult tags. How can I do this?
Edited and tested
// a bit of a hack, but let's see...
list($trash,$result)=explode('<soap:Body>',$result);
list($result,$trash)=explode('</soap:Body>',$result);
unset($trash);
$result=str_replace('xmlns="url"','',$result);
$simple_result=simplexml_load_string($result);
$json_result=json_encode($simple_result);
//var_export($simple_result);
echo $json_result;
Output:
{"GetCommunitiesResult":{"Communities":[{"CommunityID":"1","CommunityName":"
Not Specified"},{"CommunityID":"276","CommunityName":"Bella
Toscana"},{"CommunityID":"31","CommunityName":"Crescent
Lakes"},{"CommunityID":"62","CommunityName":"Hillcrest
Estate"},{"CommunityID":"750","CommunityName":"Sunny
Beach"},{"CommunityID":"124","CommunityName":"Terra Verde
Resort"},{"CommunityID":"744","CommunityName":"The Dales at West
Haven"},{"CommunityID":"158","CommunityName":"Westridge"}]}}
I have tried to looking around the web and searching a lot but I can't figure out how to do this.
I want to make a SOAP request with a header and XML code.
This is how far I have come: created a variable that holds the header info:
$headers = array(
"POST /somefile.asmx HTTP/1.1",
"Host: a ip adress",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml_post_string)
);
and the xml I want to send looking like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetViVaDataT xmlns="http://www.somesite.se/somefile.wsdl">
<PlatsId>int</PlatsId>
</GetViVaDataT>
</soap:Body>
</soap:Envelope>
But I have no idea how to go further. i'm using soap 1.2.
If other people have the same problem that I have, so here is what i did to get it to work.
$soapAction = 'http://www.somesite.com/path/to/file/file.wsdl/function';
$xml = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org /soap/envelope/">
<soap:Body>
<GetViVaDataT xmlns="http://www.somesite.se/somefile.wsdl">
<PlatsId>int</PlatsId>
</GetViVaDataT>
</soap:Body>
</soap:Envelope>';
$headers = array(
"POST /site.asmx HTTP/1.1",
"Host: a ip adress",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: ".strlen($xml),
'SOAPAction:' .$soapAction
);
$curlData = $xml;
$url='http://site/file.asmx';
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_TIMEOUT,120);
curl_setopt($curl,CURLOPT_ENCODING,'xml');
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
curl_setopt ($curl, CURLOPT_POST, 1);
curl_setopt ($curl, CURLOPT_POSTFIELDS, $curlData);
$result = curl_exec($curl);