If I used SoapClient, my code would look something like this:
$baseurl = 'http://www.webservicex.net/geoipservice.asmx?WSDL';
$client = new SoapClient($baseurl);
print_r($client->GetGeoIP(array('IPAddress'=>'123.45.67.890')));
So using $client, I can access the GetGeoIP method. How would do the same thing using cURL?
So far I've set up the code as:
$curl = curl_init($baseurl);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$rawPOSTdata = array(
"IPAddress" => "123.45.67.890",
);
// now how do I specify that I want to send $rawPOSTdata to the GetGeoIP() function?
curl_setopt($curl, CURLOPT_POSTFIELDS, $rawPOSTdata);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($curl);
Now where and how do I specify the function call GetGeoIP?
EDIT
After extending the SOAP class, this is what I got for $request
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webservicex.net/">
<SOAP-ENV:Body>
<ns1:GetGeoIP>
<ns1:IPAddress>123.45.67.890</ns1:IPAddress>
</ns1:GetGeoIP>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
So if I got this right, I should store this inside a variable and use in the
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml_string) ?
If you download and use SoapClientTimeout, and then simply change the class's __doRequest emthod and include a dump of the $request variable to figure out how SoapClient wraps it:
public function __doRequest($request, $location, $action, $version, $one_way = FALSE) {
echo htmlentities($request);
//...
}
and call it as a regular soap client:
$baseurl = 'http://www.webservicex.net/geoipservice.asmx?WSDL';
$client = new SoapClientTimeout($baseurl);
print_r($client->GetGeoIP(array('IPAddress'=>'123.45.67.890')));
..you'll get your answer:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.webservicex.net/">
<SOAP-ENV:Body>
<ns1:GetGeoIP>
<ns1:IPAddress>123.45.67.890</ns1:IPAddress>
</ns1:GetGeoIP>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
This entire XML string goes into CURLOPT_POSTFIELDS.
Related
I'm trying to connect to an API that uses SOAP. It is working for me with php/curl, but what I'd really like to do is use SoapClient, which looks like it would produce much cleaner code.
This is the php that works:
$xml_post_string = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope
/" xmlns:v300="http://V300">
<soapenv:Header/>
<soapenv:Body>
<v300:GetNote soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<Request xsi:type="get:Request" xmlns:get="http://getNote.note.V300">
<Security xsi:type="req:Security" xmlns:req="http://request.base.V300">
<Password xsi:type="xsd:string">'.$soapPassword.'</Password>
<Username xsi:type="xsd:string">'.$soapUser.'</Username>
</Security>
<NoteID xsi:type="xsd:string">'.$soapNoteID.'</NoteID>
</Request>
</v300:GetNote>
</soapenv:Body>
</soapenv:Envelope>';
$headers = array(
"Content-type: application/soap+xml;charset=utf-8",
"Accept: text/xml",
"SOAPAction: https://webservices.aus.vin65.com/V300/NoteService.cfc?wsdl",
"Content-length: ".strlen($xml_post_string),
);
$url = $soapUrl;
$ch = curl_init();
echo("set curl\n");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE,false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo("about to execute\n");
// converting
$response = curl_exec($ch);
echo('Got '.strlen($response)." characters in response\n");
curl_close($ch);
echo("Response:\n".$response."\n");
And this is my attempt at a SoapClient version, informed by this helpful StackExchange user over here: How to make a PHP SOAP call using the SoapClient class
class Security{
function Security($Username,$Password){
$this->Username = $Username;
$this->Password=$Password;
}
}
class Request{
function Request($Security,$NoteID){
$this->Security = $Security;
$this->NoteID = $NoteID;
}
}
$client = new SoapClient($soapUrl);
$security = new Security($soapUser,$soapPassword);
$securityArray = array("Username"=>$soapUser,"Password"=>$soapPassword);
//$request = new Request($security,$soapNoteID);//gave error message "expects parameter 2 to be array"
//$request = array("Security"=>$security,"NoteID"=>$soapNoteID);//gave error message "No such operation 'GetNote'"
$request = array("Security"=>$securityArray,"NoteID"=>$soapNoteID);//gave error message "No such operation 'GetNote'"
echo("Showing Request:\n");
var_dump($request);
$response=null;
try{
$response = $client->__soapCall("GetNote",$request);
}catch(Exception $ex){
echo("Caught Exception: ".$ex->getMessage()."\n");
echo("Last Request was: ".$client->__getLastRequest()."\n");
echo("Last Request Headers: ".$client->__getLastRequestHeaders()."\n");
echo("Last Response was: ".$client->__getLastResponse()."\n");
echo("Last Response Headers: ".$client->__getLastResponseHeaders()."\n");
}
if($response){
var_dump($response);
}
As you can see, I've tried a couple of different ways to feed it the user/password/NoteID information, noting down which error message it gives me each time. Currently it's claiming that there's 'no such operation GetNote' - definintely not right, but I don't know what the underlying problem is.
Thanks in advance for any pointers.
When i do $client->__getFunctions(), the method GetNote method shows up and works
<?php
$client = new SoapClient('https://webservices.aus.vin65.com/V300/NoteService.cfc?wsdl');
/**
print_r($client->__getFunctions());
Array
(
[0] => Response SearchNotes(Request $Request)
[1] => Response AddUpdateNote(Request $Request)
[2] => Response GetNote(Request $Request)
)
*/
$result = $client->GetNote([
'Security' => [
'Password' => 'XXX',
'Username' => 'XXX'
],
'NoteID' => 'XXX'
]);
print_r($result);
I want to configure an api of soap with php, following the xml is used for the api, but its not responding, please help me to recover this problem
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fram="http://framework.zend.com">
<soapenv:Header/>
<soapenv:Body>
<fram:getCountries>
<securityInfo>
<userName>username</userName>
<password>password</password>
<agentCode>code</agentCode>
<lang>en</lang>
</securityInfo>
</fram:getCountries>
</soapenv:Body>
</soapenv:Envelope>
The below showing the php code I used to call xml. But I didnt get any response to my php code, but the xml is correct, I have asked to the provider they told me the xml is correct but my php code is not correct. I dont what I do to solve this problem :(
<?php
$soapUrl = "http://testapi.roombookpro.com/en/soap/index?wsdl"; // asmx URL of WSDL
$soapUser = "username"; // username
$soapPassword = "password"; // password
// xml post structure
$xml_post_string = '<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fram="http://framework.zend.com">
<soapenv:Header/>
<soapenv:Body>
<fram:getCountries>
<securityInfo>
<!-- You may enter the following 4 items in any order -->
<userName>username</userName>
<password>password</password>
<agentCode>agentcode</agentCode>
<lang>en</lang>
</securityInfo>
</fram:getCountries>
</soapenv:Body>
</soapenv: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://testapi.roombookpro.com/en/soap/index#getCountries",
"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, 0);
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);
var_dump($parser);
echo $parser;
// user $parser to get your data out of XML response and to display it.
?>
In your code,your request is pointing to wsdl link below,
http://testapi.roombookpro.com/en/soap/index?wsdl
and thats why you are not getting soap response rather you getting the definitions.
you should use the following soap url to get the soap response,
http://testapi.roombookpro.com/en/soap/index
EDIT:
and to get the XML response (as string):
$response = curl_exec($ch);
curl_close($ch);
print_r($response);
to get as SimpleXMLElement Object :
$xml = simplexml_load_string($response);
foreach ($xml->xpath('//ns2:Soap_Model_SOAP_Location_Country') as $item)
{
print_r($item);
//to access individual elements
// echo $item->id ;
// echo $item ->code;
// echo $item->name;
// echo "\n";
}
which gives array of XML objects:
SimpleXMLElement Object
(
[id] => 252
[code] => ZW
[name] => Zimbabwe
)
I am passing an xml string to https://insurance-api.pingtree.co.uk/api through curl but is getting parameter error in response (code 12 0) which can be also seen if you browse the above mentioned url.The parameter is defined in the xml i.e. the campaign code then why isn't the system recognizing it. here is my code below which i also got from the http://www.quinternet.com site.
<?php
/**
* Initialize the request xml string here.
* The string in the tag like '[int]' should be replaced with actual value.
* The string in the square brackets is the data type.
*/
$request_string =
'<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope
xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:urn="urn:pt_service">
<soapenv:Header />
<soapenv:Body>
<urn:submit
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<campaign_code>dllfYg0JBwQ</campaign_code>
<site_url>www.surelifecover.co.uk</site_url>
<client_ip>127.0.0.1</client_ip>
<policy_type>J</policy_type>
<cover_purpose>1</cover_purpose>
<cover_amount>50000</cover_amount>
<cover_term>1</cover_term>
<title>1</title>
<first_name>INTRODUCERTEST</first_name>
<last_name>wwww</last_name>
<dob >1995-04-05</dob >
<email>will#bitscube.com</email>
<home_phone>07111111111</home_phone>
<best_call_time>1</best_call_time>
<gender>F</gender>
<is_smoker>Y</is_smoker>
<title2>1</title2>
<first_name2>ww</first_name2>
<last_name2>wwww</last_name2>
<dob2>1991-02-03</dob2>
<email2>913661383#qq.com</email2>
<home_phone2>07111111112</home_phone2>
<best_call_time2>2</best_call_time2>
<gender2>M</gender2>
<is_smoker2>N</is_smoker2>
<terms_consent>Y</terms_consent>
<privacy_content>Y</privacy_consent>
<mode>LIVE</mode>
<keyword>life ins</keyword>
<match_type>1</match_type>
<network>ABC</network>
<device>mobile</device>
<ref>00</ref>
<v1>var1</v1>
<v2>var2</v2>
<v3>var3</v3>
<v4>var4</v4>
<v5>var5</v5>
<i1>1</i1>
<i2>2</i2>
<i3>3</i3>
<i4>4</i4>
<i5>5</i5>
</urn:submit>
</soapenv:Body>
</soapenv:Envelope>
</env:Envelope>';
echo htmlspecialchars($request_string);
echo '<br>';
/**
* The values of the following fields please refer
*to the reference field list spec*
//setup the request headers here, notice at the action string and SOAPAction string */
$request_headers = array('Content-Type: application/soap+xml;charset=utf-8;action="urn:pt_service#Webservice#submit"',
'SOAPAction: "urn:pt_service#Webservice#submit"',
'Content-length: '.strlen($request_string) );
//setup the service url here
$service_url = 'https://insurance-api.pingtree.co.uk/api';
//setup the user agent for the request
$user_agent = $_SERVER['HTTP_USER_AGENT'];
//initialize the curl here
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $service_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLINFO_HEADER_OUT, FALSE );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST,TRUE );
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_POSTFIELDS,$request_string);
curl_setopt($ch, CURLOPT_HTTPHEADER,$request_headers);
//do the submit process here
$result= curl_exec ($ch);
$response_string = curl_exec($ch);
// Convert API feed to xml object
var_dump($response_string);
$xml = simplexml_load_string($response_string);
$name = $xml->response->status;
echo $name;
//-----------------------------------------------
//parsing the response string here
/*example response below. Please get detail
refer for the reponse in specs.
<?xml version="1.0" encoding="UTF8"?>
<response>
<result>20</result>
<status>1</status>
<commission>10.00</commission>
<reference>347</reference>
<message><![CDATA[Accepted]]></message>
<redirect_url>
http://insurance-api.pingtree.co.uk/redirect?p=bUzbir4VhB-BB-Z_gMxVSy8YT15gxUB9iJMBGxi_aOq5PQuDBf_JPqgvc2egv6orCj6TJkeR5g6tX1y0D7hZgbV6DdBAlzbccKmkFNG4-PgWmaNR9LQ57cUQg6jTOXuB
</redirect_url>
</response>
//-----------------------------------------------*/
curl_close($ch);
?>
Actually I create a Soap Proxy in which I get the client request and I need to post the request further to another SOAP server (with c_url).
The response is successfully obtained (as xml with <SOAP-ENV and all others).
The problem is that in my SOAP PROXY I want to return exactly the response and if my server is returning the xml the SOAP Server actually return the XML file wrap up
<SOAP-ENV:Envelope ...>
<SOAP-ENV:Body>
<ns1:loginResponse>
my xml that already contains <soap:Envelope, <soap:Body> and <namesp1:loginResponse>
</ns1:loginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The question is: How can I make the soap server to return exactly the response that i want without wrapping up with soap envelope and others?
Thanks.
UPDATED:
My soap server:
$server = new SoapServer($myOwnWsdlPath);
$this->load->library('SoapProxy');
$server->setClass('SoapProxy', $params );
$server->handle();
My soap Porxy with c_url:
public function __call($actionName, $inputArgs)
{
//some logic
$target = ...
$url = ..
$soapBody =..
$headers = ..
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $soapBody); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch); //soap xml response
curl_close($ch);
file_put_contents('/tmp/SoapCurl.txt', var_export($response, true));
return $response;
}
The response from /tmp/SoapCurl.txt is the correct one:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope ...>
<soap:Body>
<namesp1:loginResponse>
<session_id xsi:type="xsd:string">data</session_id>
</namesp1:loginResponse>
</soap:Body>
</soap:Envelope>
My soap server response is wrong:
<SOAP-ENV:Envelope ...>
<SOAP-ENV:Body>
<ns1:loginResponse>
<soap:Envelope ...>
<soap:Body>
<namesp1:loginResponse>
<session_id xsi:type="xsd:string">correct data</session_id>
</namesp1:loginResponse>
</soap:Body>
</soap:Envelope>
</ns1:loginResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
The fix I found was to extend the 'handle' function of SoapServer
Discard the output of SoapServer (with ob_end_clean) and replace it with my data
class MySoapServer extends SoapServer
{
public function handle($soap_request = null)
{
parent::handle();
ob_end_clean();
ob_start();
echo $_SESSION['data'];
}
}
I wrote a really basic wrapper class for Highrise's API. It works perfectly for Reads (GET), and I'm just starting to test it for Creates (POST). As far as I can tell, these two requests (one on the command line, one via PHP's cURL library) are identical. Same XML, same options set....just one works, and the other doesn't.
Any help is appreciated. I posted this question to 37signals developer mailing list also, but stackoverflow is generally faster at spotting my dumb mistakes...
This is the error I get with PHP's cURL (makes me think Highrise is having a problem parsing the XML string):
<?xml version="1.0" encoding="UTF-8"?> <errors> <error>First name can't be blank</error> </errors>
This is what works on the command line:
curl -u 'my_api_key:X'
-H 'Content-type: application/xml'
-d '<?xml version="1.0" encoding="UTF-8"?> <person><first-name>Savos</first-name><last-name>Aren</last-name><title>Archmage</title><company-name>Winterhold College</company-name><contact-data><email-addresses/><phone-numbers><phone-number><number>555-555-5555</number><location>Home</location></phone-number><phone-number><number>555-555-5555</number><location>Work</location></phone-number><phone-number><number>555-555-5555</number><location>Mobile</location></phone-number></phone-numbers><addresses><address><city>Winterhold</city><country>Tamriel</country><state>Skyrim</state><street>Hall of the Elements, College of Winterhold</street><zip>99999</zip><location>Work</location></address></addresses></contact-data></person>'
https://myuserid.highrisehq.com/people.xml
Here's my wrapper class:
class HighriseAPICall {
protected $_timeout = 120;
protected $_url_prefix = 'https://';
protected $_url_suffix = '.highrisehq.com';
protected $_password = 'X';
protected $_userpwd;
protected $_url;
public function __construct($api_key, $username) {
$this->_userpwd= $api_key . ':' . $this->_password;
$this->_url = $this->_url_prefix . $username . $this->_url_suffix;
}
/**
* Make the specified API call.
* #param string $action one of the four HTTP verbs supported by Highrise
* #param string $resource_name the Highrise resource to be accessed
* #param string $xml a well-formed XML string for a Highrise create, update, or delete request
*
* $xml parameter should include any query parameters as suggested by Highrise API documentation
* eg, if you want to GET all People, pass in "/people.xml"
* and if you want to get People by search term where field=value,
* then pass in "/people/search.xml?criteria[field]=value"
*/
public function makeAPICall($action,$resource_name,$xml=null) {
/* initialize curl session and set defaults for new API call */
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->_url . $resource_name);
curl_setopt($curl, CURLOPT_USERPWD, $this->_userpwd);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $this->_timeout);
/* if xml was passed in, set header and postfields */
if (isset($xml)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, 'Content-type: application/xml');
curl_setopt($curl, CURLOPT_POSTFIELDS, "$xml");
}
/* set action as custom request */
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $action);
/* get the string response from executing the curl session */
$result = curl_exec($curl);
curl_close($curl);
// return the response as a simpleXMLElement
try {
$result_simplexml = new SimpleXMLElement($result);
}
catch (Exception $e) {
throw new Exception("Highrise API Call Error: " . $e->getMessage() . ", Response: " . $result);
}
if (!is_object($result_simplexml)) {
throw new Exception("Highrise API Call Error: Could not parse XML, Response: " . $result);
}
return $result_simplexml;
}
}
?>
And the simple testpage I'm using:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
require_once('HighriseAPICall.class.php');
$highrise_api_key = 'OBSCURED';
$highrise_username = 'OBSCURED';
$highrise_api = new HighriseAPICall($highrise_api_key, $highrise_username);
$person_xml ='<?xml version="1.0" encoding="UTF-8"?> <person><first-name>Savos</first-name><last-name>Aren</last-name><title>Archmage</title><company-name>Winterhold College</company-name><contact-data><email-addresses/><phone-numbers><phone-number><number>555-555-5555</number><location>Home</location></phone-number><phone-number><number>555-555-5555</number><location>Work</location></phone-number><phone-number><number>555-555-5555</number><location>Mobile</location></phone-number></phone-numbers><addresses><address><city>Winterhold</city><country>Tamriel</country><state>Skyrim</state><street>Hall of the Elements, College of Winterhold</street><zip>99999</zip><location>Work</location></address></addresses></contact-data></person>';
$response = $highrise_api->makeAPICall('POST', '/people.xml', $person_xml);
echo htmlentities($response->asXML());
?>
</body>
</html>
try this lines instead of what you have in your script
if (isset($xml)) {
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST ,false);
}
In my wrapper class, the line:
curl_setopt($curl, CURLOPT_HTTPHEADER, 'Content-type: application/xml');
should have been:
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/xml'));