Posing XML string as parameter via cURL - php

I am trying to post a xml string to a remote perl script via cURL. I want the xml string to be posted as a post parameter 'myxml'. See the code I am using below:
$url = 'http://myurl.com/cgi-bin/admin/xml/xml_append_list_init.pl';
$xml = '<?xml version="1.0" standalone="yes"?>
<SUB_appendlist>
<SUB_user>username</SUB_user>
<SUB_pass>password</SUB_pass>
<list_id>129</list_id>
<append>
<subscriber>
<address>test#test.comk</address>
<first_name>Test</first_name>
<last_name>Test</last_name>
</subscriber>
</append>
</SUB_appendlist>';
$ch = curl_init(); //initiate the curl session
curl_setopt($ch, CURLOPT_URL, $url); //set to url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // tell curl to return data in a variable
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml", "Content-length: ".strlen($xml)));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'myxml='.urlencode($xml)); // post the xml
curl_setopt($ch, CURLOPT_TIMEOUT, (int)30); // set timeout in seconds
$xmlResponse = curl_exec($ch);
curl_close ($ch);
However the remote server is not seeing the data in the 'myxml' parameter. And I get the following response back in $xmlResponse
HTTP/1.1 200 OK
Date: Fri, 15 Apr 2011 12:00:44 GMT
Server: Apache/2.2.9 (Debian)
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html; charset=ISO-8859-1
I'm not a cURL expert by any measure so I may be doing something in mu cURL request which is obviously wrong. Would appreciate it if anyone can shed any light or spot any problems in this. Hope that is enough information.
Cheers,
Adrian.

The body of your message is not text/xml data. It is application/x-www-form-urlencoded data. You have form data containing XML, not plain XML.
Your problem is akin to trying to open MyDoc.zip in MS Word. You have to deal with it as a zip file before dealing with it as Word.
Based on my reading of the PHP manual, you want to remove:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml", "Content-length: ".strlen($xml)));
and change the POSTFIELDS line to:
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'myxml' => $xml
));

That is not the correct content type for all browsers.
see this article
sometimes the content type for xml is: application/rss+xml

Related

PHP cURL not sending headers (?)

I attempted to modify the code I found on this webpage: https://tutorialsclass.com/php-rest-api-get-data-using-curl/
The API I'm using needs two custom headers: Accept-Encodig and x-api-key.
My modified code looks like this:
$curl_handle = curl_init();
$url = "http://thelinktomyAPI";
$headers = array(
'Accept-Encoding: application/json',
'Content-Type: application/json',
'x-api-key: MyLongAPIKey'
);
// Set the curl URL option
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_handle, CURLOPT_HEADER, true);
curl_setopt($curl_handle, CURLOPT_ENCODING, 'application/json');
// This option will return data as a string instead of direct output
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$curl_data = curl_exec($curl_handle);
if ($curl_data === false)
{
print_r('Curl error: ' . curl_error($curl_handle));
}
curl_close($curl_handle);
print_r($curl_data);
So as you can see I'm making my headers visible to make sure I'm actually sending them. What I get shown is a webpage with the following:
HTTP/1.1 302 Moved Temporarily Date: Thu, 02 Dec 2021 08:09:59 GMT
Content-Type: text/html Content-Length: 110
Connection: keep-alive
Location: http://thelinktomyAPI
X-Trace-ID:f306fe34-079b-493c-b5db-073e792ec2a1
X-Kong-Response-Latency: 0
Server: kong/2.3.3 302 Found
Since the Content-Type is shown as "text/html" it leads me to believe that my headers aren't sent at all, hence I'm not getting any data. I'd appreciate any help.
Edit Adding curl_setopt($curl_handle, CURLOPT_FOLLOWLOCATION, true); as Professor Abronsius suggested helped!

Get jsonRPC data with PHP curl,

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.

Unable to decode gzip using cURL PHP

I have been trying several ways, before deciding to ask this question here... no way has succeeded with me... I'm trying to decode and read the data from one site that use gzip.
I'm using cURL & PHP. When I try to decode and print the result, I'm getting a long list of garbled special characters such as:
JHWkdsU01EUXdWa1pXYTFOdFZsZFRiaz
VoVW14S2NGbFljRmRXYkdSWVpFZEdWRT
FYVWtoWmEyaExXVlpLTm1KR1VsWmlXR2
If I run the below PHP script I got an error like:
PHP Warning: gzdecode(): data error in /var/www/mn.php on line 20
Here's my current code:
<?
$data_string = '9999';
$ch = curl_init('http://example.com/getN.php&keyword=');
curl_setopt( $ch, CURLOPT_USERAGENT, 'Darwin/15.0.0' );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch,CURLOPT_ENCODING , 'gzip');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT,5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Accept-Encoding: gzip, deflate',
'Content-Length: ' . strlen($data_string))
);
$result = gzdecode ( curl_exec($ch) );
curl_close($ch);
print_r($result);
?>
I also try to enable deflate module by:
a2enmod deflate
/etc/init.d/apache2 restart
and enable the zlib from php.ini
either I try to test it directly
curl -sH 'Accept-encoding: gzip' http://example.com/getN.php&keyword=9999 | gunzip -
I got the same result.
Here is the info from the site:
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 15 Oct 2015 00:41:54 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Powered-By: PHP/5.4.31
X-Frame-Options: SAMEORIGIN
Content-Encoding: gzip
please help
I notice your code has
curl_setopt($ch,CURLOPT_ENCODING , 'gzip');
and a gzdecode() call later on. If instructed to accept encoded content, cURL handles decoding automatically for you, without the need to manually do it after curl_exec(). Its return value is already decoded if you told cURL to accept encoded transfer.
That said, the page you are trying to download may not be actually be encoded with gzip, but another method. As stated in the manual, try specifying an empty string:
# Enable all supported encoding types.
curl_setopt($ch, CURLOPT_ENCODING, '');
This enables all supported encoding types. And don't use gzdecode(). The result should be already decoded.
thanks all ,, finally start working after I take your advice and remove gzdecode and some others and keep the header to.. Accept Encoding to gzip and here the final code
<?
$data_string = '9999';
$ch = curl_init('http://example.com/getN.php&keyword=');
curl_setopt( $ch, CURLOPT_USERAGENT, 'Darwin/15.0.0' );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT,5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow redirects
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
'Accept-Encoding: gzip',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
curl_close($ch);
print $result;
?>

Using curl, however my array will not post and content-type header does not send

Here's my code for performing the POST:
$url = "http://www.xxxxxxxxxxx.com/dfeed/index.cfm"; //where to send it
$ch = curl_init();
curl_setopt($ch, CURLOPT_VERBOSE, 1); // set url to post to
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml; charset=ISO- 8859-1'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 40); // times out after 4s
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // add POST fields
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch); // run the whole process
echo $result;
//echo curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close ($ch);
My post array starts out with the same few (manually defined) entries like this:
$data[0] = "<?xml version='1.0' encoding='ISO-8859-1'?>\n";
$data[1] = "<rss xmlns:g='http://base.google.com/ns/1.0' version='2.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:noNamespaceSchemaLocation='http://www.xxxxx.com/dFeed/schemas/FeedSchema1.0.xsd'>\n";
The rest of them are populated in a loop like this:
array_push($data, "\t<item>\n");
The response from the server indicates a different content-type from what I have set, and also indicates it received no information so something must be wrong:
HTTP/1.1 400 Bad Request Transfer-Encoding: chunked Content-Type: text/htmlServer: Microsoft-IIS/7.0 X-XXX Server: Web2 Date: Mon, 25 Jun 2012 12:49:53 GMT Bad Request
Error: Not XML
Content received:
The charset looks like it has whitespace in it or something, not sure if that was just on the way into SO or actually in your code.
Do you really need to add \t and \n around the place, the server won't care if you don't.
Try implode()ing the post array and just sending a string of XML.
No definitive answers but hopefully something in the right direction.
Let us know how you get on, good luck.

Problem when posting a file with curl in php

I'm trying to post a file with curl in php, but the file is never uploaded/accepted by the server. I have searched and tried for several hours, but I can't find whats wrong, everyone elses examples and codes seems to work, but not this one.
Here is the code:
<?php
$url = "http://jpptst.ams.se/0.52/default.aspx";
$headers = array(
"Content-Type: text/xml; charset=iso-8859-1",
"Accept: text/xml"
);
$data = array("file" => "#documents/xmls/1298634571.xml");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
?>
The result I get:
string(904) "HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Mon, 25 Jul 2011 19:13:41 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 659"
Thats all I get.. the file is never accepted by the server.
If anyone can help me with this problem it would be much appreciated :)
Thanks!
You're trying to upload a file via HTTP post, so sending a Content-type: text/xml header is inappropriate. An HTTP file upload is actually done as multipart/form-data, and is actually pretty much identical to a MIME-encoded email attachment. PHP's curl will fill in the header details for you automatically. As well, the Accept header is not necessary either.
Check that the path to the .xml file you're trying to upload is correct. You've not specified a leading / to it, so the path is relative to where your PHP script is executing from.
Replace:
$data = array("file" => "#documents/xmls/1298634571.xml");
With this:
$data = array("file" => "#".realpath('documents/xmls/1298634571.xml'));
Try it, might work, i'm not sure tho.
EDIT:
Try this out:
<?php
$xmldatafile="documents/xmls/1298634571.xml"; // Make sure the file path is correct
function postData($postFields,$url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS ,$postFileds);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$xmlData = file_get_contents($xmldatafile);
$postFileds = 'data='.$xmlData;
$result = postData($postFields,"http://jpptst.ams.se/0.52/default.aspx");
?>

Categories