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!)
Related
I am trying to write some code to connect a webpage to an external API. The API's documentation tells me that the request should be done with a SOAP envelope, which should look like this:
<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetNote>
<Request>
<Security>
<Username>username</Username>
<Password>password</Password>
</Security>
<NoteID>noteID</NoteID>
</Request>
</GetNote>
</soap:Body>
</soap:Envelope>
Using some sample code created by a helpful StackOverflow user here: SOAP request in PHP with CURL I then turned it into this (slightly redacted) php:
<?php
$soapUrl = 'https://www.blah.com/DoodadService.cfc?wsdl';
$xml_post_string = '<soap:Envelope xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetNote>
<Request>
<Security>
<Username>'.$soapUser.'</Username>
<Password>'.$soapPassword.'</Password>
</Security>
<NoteID>'.$soapNoteID.'</NoteID>
</Request>
</GetNote>
</soap:Body>
</soap:Envelope>';
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"SOAPAction: https://www.blah.com/DoodadService.cfc?wsdl",
"Content-length: ".strlen($xml_post_string),
);
$url = $soapUrl;
$ch = curl_init();
echo('set curl<br>');
$f = fopen('request.txt','w');
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,true);
curl_setopt($ch, CURLOPT_STDERR, $f);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo('about to execute<br>');
// converting
$response = curl_exec($ch);
fclose($f);
echo('Got '.strlen($response).' characters in response<br>');
curl_close($ch);
echo('Saved error strings to request.txt<br> ');
?>
The result was a Version Mismatch error, ie:
<soapenv:Fault>
<faultcode>
soapenv:VersionMismatch</faultcode>
<faultstring>
Version Mismatch</faultstring>
<detail>
<ns1:hostname xmlns:ns1="http://xml.apache.org/axis/">
ip-10-4-4-149</ns1:hostname>
</detail>
</soapenv:Fault>
Can anyone tell me what this error means, what is being "mismatched" against what, and how I might be able to fix it up?
Thanks!
You could try to change the Content-type header to a SOAP+XML content. The charset doesn't need quotation.
"Content-type: application/soap+xml; charset=utf-8"
Otherwise, surely you have missed something that you don't know.
You could find some examples using this API or similar cases with SOAP in another SO questions.
As it turns out, the issue was "online manual giving out of date information". Not a problem that general SOAP experts can help out with after all!
In course of consultation with the API providers, was pointed at a useful tool SoapUI - https://www.soapui.org/ - which, as it turns out, can be used to generate correct Soap envelopes given the API's URL - meaning that I am no longer dependent on this API's providers to have their documentation up to date. I recommend it to anyone else who finds themselves in a similar fix.
Consider following URL:
click here
There is some encoding into Japanese characters. Firefox browser on my PC is able to detect it automatically and show the characters. For Chrome, on the other hand, I have to change the encoding manually to "Shift_JIS" to see the japanese characters.
If I try to access the content via PHP-cURL, the encoded text appears garbled like this
���ϕi�̂��ƂȂ��I�݂��Ȃ̃N�`�R�~�T�C�g�������������i�A�b�g�R�X���j�ɂ��܂����I
I tried:
curl_setopt($ch, CURLOPT_ENCODING, 'Shift_JIS');
I also tried (after downloading the curl response):
$output_str = mb_convert_encoding($curl_response, 'Shift_JIS', 'auto');
$output_str = mb_convert_encoding($curl_response, 'SJIS', 'auto');
But that does not work either.
Here is the full code
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Connection: keep-alive'
));
//curl_setopt($ch, CURLOPT_ENCODING, 'SJIS');
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
That page doesn't return valid HTML, it's actually Javascript. If you fetch it with curl and output it, add header('Content-type: text/html; charset=shift_jis'); to your code and when you load it in Chrome the characters will display properly.
Since the HTML doesn't specify the character set, you can specify it from the server using header().
To actually convert the encoding so it will display properly in your terminal, you can try the following:
Use iconv() to convert to UTF-8
$curl_response = iconv('shift-jis', 'utf-8', $curl_response);
Use mb_convert_encoding() to convert to UTF-8
$curl_response = mb_convert_encoding($curl_response, 'utf-8', 'shift-jis');
Both of those methods worked for me and I was able to see Japanese characters displayed correctly on my terminal.
UTF-8 should be fine, but if you know your system is using something different, you can try that instead.
Hope that helps.
The following code will output the Japanese characters correctly in the browser:-
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $setUrlHere);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
// grab URL content
$response = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
header('Content-type: text/html; charset=shift_jis');
echo $response;
I'm using the following function to send data to a particular API.
function api_post($xml) {
$ch = curl_init('http://api.asmx');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml;charset=UTF-8"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
$results = curl_exec($ch);
return $results;
}
The output is
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<LoadResponse xmlns="http://api.api.com/">
<LoadResult>
<date>2015-09-18T10_07_51.997</date>
<data><br>⢠bullet1<br>⢠Bullet2</data>
</LoadResult>
</LoadResponse>
</soap:Body>
</soap:Envelope>
The results returned are as expected except that bullet points are returned as â¢and date values as 2015-09-18T10_07_51.997 instead of 2015-09-18T10:07:51.997.
When I test out the same API call with the same XML in Soap UI everything is returned accurately. I'm assuming I have some kind of encoding issue in PHP. How can I resolve?
Depending on the remote encoding, you can use:
return utf8_encode($results);
You can also try
return utf8_decode($results);
NOTE:
If you plan to output utf-8 to a browser, you can also use the following :
<?php
//Teel the browser we'll be outputting UTF-8
header('Content-Type: text/html; charset=UTF-8');
// Tell PHP that we're using UTF-8 strings until the end of the script
mb_internal_encoding('UTF-8');
// Tell PHP that we'll be outputting UTF-8 to the browser
mb_http_output('UTF-8');
Update based on you comment:
Try setting CURLOPT_ENCODING to "" (empty) and remove CURLOPT_HTTPHEADER, i.e.:
curl_setopt($ch, CURLOPT_ENCODING, "");
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'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?