I've a problem with PHP function curl_exec(). I'm sending a SOAP request to an advice and the advice should return an SOAP reponse, but I just get a String without XML tags.
Okay, I'm using this code:
protected function _send_request($url,$post_string) {
$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, $post_string);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($post_string) ));
if ( ($result = curl_exec($soap_do)) === false) {
echo curl_error($soap_do);
}
else {
echo("<pre>".$result."</pre>");
}
}
And the code seems to work. I captured the network traffic with Wireshark and the reponse in the TCP package is:
<?xml version='1.0' encoding='utf-8'?>
<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:tds=\"http://www.onvif.org/ver10/device/wsdl\" xmlns:tt=\"http://www.onvif.org/ver10/schema\">
<s:Body>
<tds:GetSystemDateAndTimeResponse>
<tds:SystemDateAndTime>
<tt:DateTimeType>
Manual
</tt:DateTimeType>
<tt:DaylightSavings>
true
</tt:DaylightSavings>
<tt:TimeZone>
<tt:TZ>
CET-1CEST,M3.5.0,M10.5.0/3
</tt:TZ>
</tt:TimeZone>
<tt:UTCDateTime>
<tt:Time>
<tt:Hour>
13
</tt:Hour>
<tt:Minute>
26
</tt:Minute>
<tt:Second>
21
</tt:Second>
</tt:Time>
<tt:Date>
<tt:Year>
2014
</tt:Year>
<tt:Month>
2
</tt:Month>
<tt:Day>
5
</tt:Day>
</tt:Date>
</tt:UTCDateTime>
<tt:LocalDateTime>
<tt:Time>
<tt:Hour>
14
</tt:Hour>
<tt:Minute>
26
</tt:Minute>
<tt:Second>
21
</tt:Second>
</tt:Time>
<tt:Date>
<tt:Year>
2014
</tt:Year>
<tt:Month>
2
</tt:Month>
<tt:Day>
5
</tt:Day>
</tt:Date>
</tt:LocalDateTime>
</tds:SystemDateAndTime>
</tds:GetSystemDateAndTimeResponse>
</s:Body>
</s:Envelope>
Now the problem. echo $result just returns
ManualtrueCET-1CEST,M3.5.0,M10.5.0/3134622201425144622201425
The XML structure seems lost and I can't recreate it. I'm working with XAMPP PHP version 5.3.1 and the curl extension. Does anybody have an idea how to get the original XML structure?
Related
Setting up a JSON-RPC on my vps which I want to connect via PHP CURL on my website doing a basic request and looking for getmasternodecount.
Tried many scripts and libraries before however none seems to work in my case. Now I try to write some basic php code, but this skill isnt my best.
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
function coinFunction () {
$feed = 'http://user:pass#ip/';
$post_string = '{"method": "getmasternodecount", "params": []}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $feed);
curl_setopt($ch, CURLOPT_PORT, port);
curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/stratum', 'Content-length: '.strlen($post_string)));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-length: '.strlen($post_string)));
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$data = coinFunction();
var_dump($data);
echo $data;
?>
And gives me this data dump:
string(127) "HTTP/1.1 403 Forbidden Date: Sun, 24 May 2020 00:06:21 GMT Content-Length: 0 Content-Type: text/html; charset=ISO-8859-1 " HTTP/1.1 403 Forbidden Date: Sun, 24 May 2020 00:06:21 GMT Content-Length: 0 Content-Type: text/html; charset=ISO-8859-1
When i delete all the var dump information etc, it send me a whitepage and sometimes NULL.
Kindly Regards,
Let's work with the first snippet. Since it's a POST request, file_get_contents is rather out of place here. Add the following setopt lines:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
Without those, the result of curl_exec won't contain the returned content.
It would also be advisable to specify the Content-Type of the request (which is application/json). The server might handle it even without, but just in case:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json'));
Authentication is another thing. Credentials in the URL suggest Basic, but the server might expect otherwise... See CURLOPT_HTTPAUTH.
I have to call a SOAP with this structure (obtained with SOAPUI):
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:v3='http://v3.ws.server.ldap.ws.xxx/'
xmlns:cod='http://server/Usr/codeUsr'>
<soapenv:Header/>
<soapenv:Body>
<v3:getUsr>
<cod:codeUsr>P012997</cod:codeUsr>
</v3:getUsr>
</soapenv:Body>
</soapenv:Envelope>
The client is:
$client = new SoapClient("http://xxx.xxx.xxx.xxx:8080/ws-ldap3/wServiceV3?wsdl");
So far, I tried:
echo "a(TT).<br/>";var_dump($client->getUsr('P012997'));
echo "b(TT).<br/>";var_dump($client->getUsr(array('cod' => 'P012997')));
echo "c(TT).<br/>";var_dump($client->getUsr(array('codeUsr' => 'P012997')));
echo "d(TT).<br/>";var_dump ($client->__soapCall('getUsr', array('parameters' => array('cod' => 'P012997'))));
echo "e(TT).<br/>";var_dump ($client->__soapCall('getUsr', array('parameters' => array('codeUsr' => 'P012997'))));
echo "f(TT).<br/>";var_dump ($client->__soapCall('getUsr', array('cod' => 'P012997')));
echo "g(TT).<br/>";var_dump ($client->__soapCall('getUsr', array('codeUsr' => 'P012997')));
without success. How can I pass the parameter codeUsr?
TIA,
I finally solved the problem (not the case) using CURL. I send the XML exactly as needed.
$xml="<same as before>";
$sDo = curl_init();
curl_setopt($sDo, CURLOPT_URL, "http://xxx.xxx.xxx.xxx:8080/ws-ldap3/wServiceV3?wsdl");
curl_setopt($sDo, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($sDo, CURLOPT_TIMEOUT, 10);
curl_setopt($sDo, CURLOPT_RETURNTRANSFER, true);
curl_setopt($sDo, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($sDo, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($sDo, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($sDo, CURLOPT_POST, true );
curl_setopt($sDo, CURLOPT_POSTFIELDS, $xml);
curl_setopt($sDo, CURLOPT_HTTPHEADER, array("Content-Type: text/xml; charset=utf-8", "Content-Length: " . strlen($xml)));
$result = curl_exec($sDo);
Post it in case somebody else have a bottleneck and can't spend days to figure how to make it work.
Hi "I would like to ask an assistance" yes, you read it right, I don't know what's happening to my cURL development program. Straight to the point, I have cURL development program which works perfect and faster in my localhost xampp, I even receives a response correctly. But I thought i'm done on my work, until I uploaded it to my cpanel, It became superslow in terms loading, It cannot receives any response anymore also after loading.
I just receive
Curl Error: 7
OR
Curl Error: 28
I don't know whats this weird thing. I googled already the errors (cURL error(7) "It says connection issue to host, but its impossible, I can connect to host in my localhost." cURL error: 28 I have made some adjustment also to my curl_opt() you see in my comment below.
Here's my code
$headers = array(
// "Accept-Encoding: gzip, deflate",
"Content-Type: text/xml;charset=\"UTF-8\"",
"SOAPAction: \"http://domain.org/\"",
"Host: domain.com",
"Content-length: ".strlen($xml_post_string),
);
$url = $soapUrl;
// PHP cURL for https connection with auth
$soap_do = curl_init() or die('Error');
set_time_limit(0);
curl_setopt($soap_do, CURLOPT_URL, $url);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 0);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 400);
curl_setopt($soap_do, CURLOPT_AUTOREFERER, true); // try, to solve an issue
curl_setopt($soap_do, CURLOPT_MAXREDIRS, 10); // try, to solve an issue
curl_setopt($soap_do, CURLOPT_FOLLOWLOCATION, true); // try, to solve an issue
curl_setopt($soap_do, CURLOPT_TIMEOUT_MS, 400); // try, to solve an issue
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, $xml_post_string);
curl_setopt($soap_do, CURLOPT_VERBOSE, TRUE);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers);
But still the same, No response coming and loads slow
Whenever I run into problems with cURL, I place the curl handle that has just completed to PHP's curl_getinfo function.
Try adding this code just after you run curl_exec($soap_do);
echo '<pre>'; print_r(curl_getinfo($soap_do)); echo '</pre>';
curl_getinfo returns an array of data, which is displayed by print_r, and formatted in an easy to read list by the <pre> tag.
I am trying to invoke a webservice uisng curl. I am receiving "connection timed out" error while running this script. Please assist me to resolve this issue.
Client:
<?php
#parameters
$strParams = '';
$strAPIname = 'CordecAPI';
$url = 'http://86.17.13.109:81/Webbooker';
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<SOAPENV:Envelope
xmlns:SOAPENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:SOAPENC="http://www.w3.org/2003/05/soap-encoding"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAPENV:Body>
<JobRequest xmlns="http://81.105.223.86:80/cni">
<SourceSystem>KVCARS</SourceSystem>
<SourcePassword>Ketchup96</SourcePassword>
<SourceJobID>*KV001*</SourceJobID>
<SourceAccount>CORDIC</SourceAccount>
<TargetSystem>TARGET1</TargetSystem>
<Lifetime>60</Lifetime>
<DriverNotes>Please wait at reception.</DriverNotes>
<OperatorNotes>Test job for CNI.</OperatorNotes>
<BookerName>Jane</BookerName>
<BookerPhone>01954233255</BookerPhone>
<BookerEmail>jane.test#cordic.com</BookerEmail>
<StopList>
<Stop>
<Order>1</Order>
<Passenger>Fara Arani</Passenger>
<Address>Cordic Ltd, 1 Rowles Way, Swavesey, Cambridge</Address>
<Postcode>CB24 4UG</Postcode>
<ContactPhone>01954233255</ContactPhone>
<ContactOnArrive>Ring</ContactOnArrive>
</Stop>
<Stop>
<Order>2</Order>
<Address>Heathrow Airport, Terminal 4</Address>
<Postcode>TW6 3GA</Postcode>
</Stop>
</StopList>
<AttributeList>
<Attribute>Executive</Attribute>
<Attribute>Professional</Attribute>
</AttributeList>
</JobRequest>
</SOAPENV:Body>
</SOAPENV:Envelope>';
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
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, $xml);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($xml) ));
//curl_setopt($soap_do, CURLOPT_USERPWD, $user . ":" . $password);
$result = curl_exec($soap_do);
$err = curl_error($soap_do);
$inf = curl_getinfo($soap_do);
if(!$result)
{
echo "CURL FAIL: $url TIMEOUT=10, CURL_ERRNO=$err";
echo PHP_EOL . '<pre>' . PHP_EOL;
var_dump($inf);
echo PHP_EOL . '</pre>' . PHP_EOL;
}
echo $result;
echo $err;
?>
If you have any example in soap. Please share with me, but I am more comfortable with curl methods.
Thanks !
You must set the header with SOAPAction: ""
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('SOAPAction: ""'));
Hi, Solution resolved,
//curl_setopt($soap_do, CURLOPT_USERPWD, $user . ":" . $password);
I just put the user name and password information and its working now
Denil:
Your comments were also helpful, thank you very much for helping. and thanks to stackoverflow Team for this portal.
Best Regards!
Ali Raza
+92 336 5141224
I am trying to call a .net webservice using Curl from PHP.
Everytime I try to do it, I get an error with the encoding. It says that utf...8 is not supported. When I check tcpdump (PacketPeeper), I see that for some reason curl is converting the string "utf-8" to "utf...8". Actually, it is converting all "-"s to "..."s.
Any idea why?
$req = "<?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>
<GenerateLink>
..............
</GenerateLink>
</soap:Body>
</soap:Envelope>";
$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, "http://www.myurl.com/serv.asmx" );
curl_setopt($soap_do, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_POST, true );
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $req);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset: utf-8' ,'SOAPAction: "http://www.myurl.com/serv.asmx"', 'Content-Length: '.strlen($req) ));
$response = curl_exec($soap_do);
I had this when I copy-pasted code among applications, and while pasting (usually with Office application) hyphens got changed into non-breaking-hyphens or such characters that are hard to tell apart from a simple hyphen. I used a hex editor to look at the file where the string (part) was stored that would be given to CURL. You should check ord($req{33})==45 before exploding while debugging! (Check me on the index 33, it was just a fast count!)