Curl request - invalid xml request when posting user&password after xml - php

I need to send the following request, which is XML with a username and password on the end of the xml. I can get this to work as a URL when I paste into the browser:
URL?XML=<>...</>&user=123456&password=123456
But when I try to create the curl call it says it's an invalid XML request.
I've tried having the user&password within the POSTFIELDS. I've also tried setting them as a variable before $trial ='&user=123456&password=123456' but still can't seem to get it to work.
$xml = <<<ENDOFXML
<?xml version="1.0" encoding="UTF-8"?><xmldata>...</xmldata>
ENDOFXML;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode($xml).urlencode('&user=123456&password=123456'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_close($ch);

This is wrong:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode($xml).urlencode('&user=123456&password=123456'));
urlencode only the variable names and the values (or only values if you know the variable names are ok):
curl_setopt($ch, CURLOPT_POSTFIELDS, 'xml='.urlencode($xml).'&user=' . urlencode('123456').'&password=' . urlencode('123456'));

Related

PHP curl only sending POST data once

I have the following PHP code:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://10.0.0.99/api/login");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "somethingsomething");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/plain',
'Content-Length: 18')
);
$cookie = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://10.0.0.99/api/getcmds");
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIE, "cookie=".$cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/plain',
'Content-Length: 0')
);
$response = curl_exec($ch);
$json = json_decode($response, true);
curl_setopt($ch, CURLOPT_URL, "https://10.0.0.99/api/cmd/" . $json["value"]);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIE, "cookie=".$cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "on");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: text/plain',
'Content-Length: 2')
);
curl_exec($ch);
curl_close($ch);
The first call to curl_exec posts the string as expected and returns the response. Second call also works as expected.
The third call however doesn't post the string "on" to the server. It correctly gets the returned data (which, without the "on" is an error), but the server never receives the posted data.
The code itself is running on PHP 5.5.30 in the command line using the -f flag. Curl Verbose mode is showing that the Content-Length is beeing set correctly, however it doesn't show anything about posted data. The rest of the post seems to be correct, it's just that no data arrives.
(PS: This is not supposed to be production code, just something I'm playing around with, so please no comments about code quality/security.)
You can try closing the connection and then starting again for a new call/request. I personally do this thing.
From php.net docs on cURL: PHP: curl_setop
As for CURL_POSTFIELDS:
This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.
In your case, you set an empty variable $_POST['on']. If you want it to have some particular value:
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "on=value");

Ingram micro integration using PHP

I am trying to post XML request to ingram micro url the code shown below
<?php
$url = "https://newport.ingrammicro.com/mustang";
$post_string = '<BaseRateRequest>
<Version1.0></Version1.0>
<TransactionHeader>
<CountryCode>FT</CountryCode>
<LoginID>username</LoginID>
<Password>password</Password>
<TransactionID>TESTAIC12356</TransactionID>
</TransactionHeader>
<BaseRateInformation>
<BranchOrderNumber>4066000</BranchOrderNumber>
<PostalCode>L5R1V4</PostalCode>
<Suffix />
</BaseRateInformation>
</BaseRateRequest>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "XML=".$post_string);
$data = curl_exec($ch);
print_r($data);
?>
But, I don't know how to request and response in XML. I am getting the error: Invalid Inbound XML Document.
What does that means and how to solve it ?
you have to remove "XML="
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
and probably also add the XML header:
<?xml version="1.0" encoding="UTF-8"?>
<BaseRateRequest>
...
</BaseRateRequest>

XML response POST request with CURL and PHP

I have to invoke a soap web service using curl and php.
I tried this code:
$ch = curl_init();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_MUTE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd................</soapenv:Envelope>");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
This code works well but I can't to print the result as XML.
If I use other client to invoke the same service I obtain a XML response (this is correct). Why?

Get currency exchange rate from bank site

I'm trying to get content from bank site using curl.
http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna
Site is specific becouse it using ajax to fill currency exchange table. There is a link for download data in to file but you have to have same session id to able to do that.
Im trying this code:
$url="http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna";
$useragent = $_SERVER['HTTP_USER_AGENT'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_URL,$url);
$cl = curl_exec($ch);
$dom = new DOMDocument();
#$dom->loadHTML($cl);
#$link = $dom->getElementById('tecajPrn');
echo $suburl = "http://www.zaba.hr".$link->getAttribute('href');
After this I got link to file but I can't open it.
Another strange situation is that link I got with curl is http://www.zaba.hr/home/ZabaUtilsWeb/utils/tecaj/danasPrn but real link when I click on icon is http://www.zaba.hr/ZabaUtilsWeb/utils/tecaj/prn/62/2014
You are messing with cookie and ajax(may be!). Here is the lookaround. Try this:
First send a request to the page to obtain the cookie.
$url="http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "mozilla 5.0");
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR,"cookie.txt");
$cl = curl_exec($ch);
curl_close($ch);
After that make another curl request. This time to obtain the json data:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "mozilla 5.0");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Requested-With: XMLHttpRequest", "Referer: http://www.zaba.hr/home/wps/wcm/connect/zaba_hr/zabapublic/tecajna"));
curl_setopt($ch, CURLOPT_URL,"http://www.zaba.hr/ZabaUtilsWeb/utils/tecaj/danas");
curl_setopt($ch, CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR,"cookie.txt");
$cl = curl_exec($ch);
curl_close($ch);
Your json is available at this variable. Parse it using json_decode()
// now parse json from $cl
print $cl;
Anything required, help yourself straightway!
Note: Make sure you have write permission for the cookie.txt file. Also, its better to use absolute path like c:/test/cookie.txt or /var/tmp/cookie.txt.

logging into twitter with curl

I'm trying to login into twitter with php + curl, but I think there's something wrong with my request because I get this as response:
Something is technically wrong.
Thanks for noticing—we\'re going to fix it up and have things back to
normal soon.
The php code I'm using is:
<?php
$ch = curl_init($sTarget);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $_CKS);
curl_setopt($ch, CURLOPT_COOKIEFILE, $_CKS);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_REFERER, "https://twitter.com");
?>
$sPost looks like this:
session[username_or_email]=user&session[password]=password&remember_me=1&scribe_log=&redirect_after_login=&authenticity_token=6e706165609354bd7a92a99cf94e09140ea86c6f
The code first fetches the login form fields so that the post variables do have the proper values(auth token). I just tried about everything and yes I know there's an api for this but I'd rather find out how to do it manual for learning's sake.
Thanks in advance.
The CURLOPT_COOKIESESSION is used to indicate a new session. That's not what you want, since you need to send the session cookie in the second post.
I got the twitter login to work with this code:
<?php
# First call gets hidden form field authenticity_token
# and session cookie
$ch = curl_init();
$sTarget = "https://twitter.com/";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($ch, CURLOPT_REFERER, "https://twitter.com/");
$html = curl_exec($ch);
# parse authenticity_token out of html response
preg_match('/<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token"\/>/', $html, $match);
$authenticity_token = $match[1];
$username = "your#email.com";
$password = "password";
# set post data
$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";
# second call is a post and performs login
$sTarget = "https://twitter.com/sessions";
curl_setopt($ch, CURLOPT_URL, $sTarget);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
# display server response
curl_exec($ch);
curl_close($ch);
?>
PS: Sorry for not reading your post properly the first time.
I noticed two things:
1) Try to URL encode your POST data
such as:
session%5Busername_or_email%5D=user&session%5Bpassword%5D=password...
instead of:
session[username_or_email]=user&session[password]=password...
2) twitter has a hidden field named authenticity_token in the login form. It is bound to the session. Thus you cannot use a static authenticity_token, you have to read the login form first and use the authenticity_token field from there.
Hope that helps.

Categories