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);
?>
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'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);
?>
I have a code here and can get only current oil price but I need to get a result the same as the picture below. I have no idea to do this.
<?php
$soapUrl = "http://www.pttplc.com/webservice/pttinfo.asmx?op=CurrentOilPrice";
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<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">
<soap12:Body>
<CurrentOilPrice xmlns="http://www.pttplc.com/ptt_webservice/">
<Language>string</Language>
</CurrentOilPrice>
</soap12:Body>
</soap12:Envelope>';
$headers = array(
"POST /webservice/pttinfo.asmx HTTP/1.1",
"Host: www.pttplc.com",
"Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: " . strlen($xml_post_string)
);
$url = $soapUrl;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
$response1 = str_replace("<soap:Body>", "", $response);
$response2 = str_replace("</soap:Body>", "", $response1);
$parser = simplexml_load_string($response2);
echo $parser->asXML();
You can use SOAP CLIENT is a good tool for use if you work with Web services. This is easy to use, automaticaly convert your response in SimpleXMLObject and you are able to use XPath to find easy your XML tag.
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);
Since the SOAP manual on php.net is not very noob friendly and I could not find any good examples I will post my question here.
How can I create PHP SOAP request to look like this?
POST /MySERVER/myWSDLservice.asmx HTTP/1.1
Host: connection.mywebsite.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://connection.mywebsite.com/MySERVER/GetCarType"
<?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>
<GetCarType xmlns="http://connection.mywebsite.com/MySERVER/">
<IDNumber>string</IDNumber>
</GetCarType>
</soap:Body>
</soap:Envelope>
Please note:
there is user/pass auth
SSL connection
Any suggestion / links / example much appreciated.
Tested and working!
with https, user & password
<?php
//Data, connection, auth
$dataFromTheForm = $_POST['fieldName']; // request data from the form
$soapUrl = "https://connecting.website.com/soap.asmx?op=DoSomething"; // asmx URL of WSDL
$soapUser = "username"; // username
$soapPassword = "password"; // password
// xml post structure
$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>
<GetItemPrice xmlns="http://connecting.website.com/WSDL_Service"> // xmlns value to be set to your WSDL URL
<PRICE>'.$dataFromTheForm.'</PRICE>
</GetItemPrice >
</soap:Body>
</soap:Envelope>'; // data from the form, e.g. some ID number
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: http://connecting.website.com/WSDL_Service/GetPrice",
"Content-length: ".strlen($xml_post_string),
); //SOAPAction: your op URL
$url = $soapUrl;
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
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);
// converting
$response = curl_exec($ch);
curl_close($ch);
// converting
$response1 = str_replace("<soap:Body>","",$response);
$response2 = str_replace("</soap:Body>","",$response1);
// convertingc to XML
$parser = simplexml_load_string($response2);
// user $parser to get your data out of XML response and to display it.
?>