How to Post Action automatically in php? - php

Details are here:
First i will hit on this link eg. http://your.app/callback?code=abc123
then i will receive value from code variable from url then i want to redirect this url as a POST action in https://api.another.com/token
grant_type=authorization_code&
code=[CODE] url, bearing the value of code

Use Curl:
An example:
<?php
// if you get the code here from the url,
$code = $_GET['code'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://api.another.com/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"token_grant_type=authorization_code&code=" + $code);
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
?>

Related

Post data from Android App to Backend PHP

If i assign a value to a variable and pass it to url, it is working fine.
$waybill=508518;
$url = 'http://api.ecomexpress.in/track_me/api/mawbd/?awb='.$waybill.'&order=&username=cs#subhashreeinfotech.com&password=####';
$ch = curl_init(); // initiate curl
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
$output = curl_exec ($ch);
But what i need is , i have to get the waybill number dynamically from android app.For that i am posting the waybill value to back end php code like below.But its not working
$waybill=$_POST['bill'];
$url = 'http://api.ecomexpress.in/track_me/api/mawbd/?awb='.$waybill.'& order=&username=cs#subhashreeinfotech.com&password=####';
$ch = curl_init(); // initiate curl
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in string format
$output = curl_exec ($ch);
Error : undefined index bill
Solved the issue using the following code
$waybill=$_POST['srt'];
ob_start();
echo $_POST['srt'];
$my = ob_get_contents();
$url='http://52.66.178.241//track_me/api/mawbd/?
awb='.$my.'&order=&username=subhashree54396&password=su54nbh39tmed';
$ch = curl_init(); // initiate curl
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return the output in
string format
$output = curl_exec ($ch);
print_r($output);
curl_close($ch);

after curl post, how to check the postfield - to debug

i have a curl post somehing like this
curl_setopt($ch, CURLOPT_URL,$cpUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $varpost); // post parameters
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return the output in string format
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_NOBODY, true);
$output = curl_exec ($ch); // Execute
how can i check after curl_close ($ch); what post field variables were sent? Please help
how can i check after curl_close ($ch); what post field variables were
sent?
Your script provides the variables sent with the CURLOPT_POSTFIELDS options, so if the cURL executed successfully, you already have those variables:
...
$output = curl_exec($ch); // Execute
//show the POST fields if they were sent successfully
if($output) print_r($varpost);
else echo "cURL failed: no POST fields sent";
You can use Fiddler to examine what data is actually beeing transfered between the client and the server

php curl function not giving the required result

I need help with API Integration. When I echo the variable $url, I get the result, but I do not know why cURL is not working for me:
<?php
$token="43e6c623dda8f35df4bXXXfa5f0ec57d58e91154a ";
$format="json";
$waybill="974510010010";
$ref_nos="";//either this or waybill
$verbose="0";// meta info need to append in url
$url="https://test.delhivery.com/api/packages/json/?token=".$token."&format=".$format."&waybill=".$waybill."&ref_nos=".$ref_nos."&verbose=".$verbose;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
echo curl_error($ch);
$return = curl_exec ($ch);
curl_close ($ch);
echo $return;
?>
Do you have access to the service you are connecting to?
It could be that the service requires the data as POST variables as opposed to the GET parameters you are sending.
Or perhaps some error handling on server that does not validate the waybill value you are sending. The ref_nos variable is also empty, which some applications could interpret as invalid. A tips would be to omit the ref_nos variable from the request string if its value is empty.

cUrl login is not working

I'm trying to login to one of my sites using curl to pull information off the page. It does not seem to be working. Here's the code I'm trying. If it helps I can create a user/pass for just this scenario.
<?php
$username = 'xxx';
$password = 'xxx';
$loginUrl = 'http://gwintersdev.com/user';
$finalUrl = 'http://gwintersdev.com/admin';
$userinput = 'name';
$passwordinput = 'pass';
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_URL,$loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$userinput=$username&$passwordinput=$password");
curl_setopt($ch, CURLOPT_USERAGENT, 'user-agent');
ob_start(); // prevent any output
curl_exec ($ch); // execute the curl command
ob_end_clean(); // stop preventing output
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL, $finalUrl);
$buf2 = curl_exec ($ch);
curl_close ($ch);
print $buf2;
?>
Update: I was able to get the above working, but I'm trying this on a different ASP site and it's not working. I grabbed all the hidden fields and added them to the post string, but it still won't login.
<?php
$username = 'xxx';
$password = 'xxx';
$loginUrl = 'http://vitalstim.com/health_professionals/certified_provider_resources/forum.aspx';
$finalUrl = 'http://vitalstim.com/health_professionals/certified_provider_resources/forum.aspx';
$userinput = 'ctl00$ContentPlaceHolder1$uc_login$txtUser';
$passwordinput = 'ctl00$ContentPlaceHolder1$uc_login$txtPass';
$login = 'ctl00$ContentPlaceHolder1$uc_login$butLogin';
$validation_input = '__EVENTVALIDATION';
$validation_input_value = '/wEWAgKf+PTrBQKItpn5BDXHCHsANbEpwkEBmMyNv+32L2Ec';
$view_state = '/wEPDwUJLTQyMjg0NzI0D2QWAmYPZBYGAgEPZBYEAgYPFgIeB1Zpc2libGVoZAIHDxYCHwBoZAIDD2QWBAIBD2QWCAIBD2QWBAIBDw8WAh4EVGV4dGVkZAIFDw8WAh8AaGRkAgcPZBYCAgEPZBYCAgMPZBYCAgEPFgIfAGhkAgkPDxYCHwBoZGQCCw8PFgIfAGhkZAIDDxYCHwBoZAIFDw8WAh8BBXY8c2NyaXB0IGxhbmd1YWdlPSJqYXZhc2NyaXB0IiB0eXBlPSJ0ZXh0L2phdmFzY3JpcHQiPgokKGRvY3VtZW50KS5yZWFkeShmdW5jdGlvbigpIHsKVml0YWxTdGltLkluaXQoNCk7Cn0pOwo8L3NjcmlwdD4KZGRkdz/7+FcQ1E1sbC0Gua3jJsCGSnM=';
$event_valid = '/wEWBwKeiM4xAoi2mfkEAurz/r4MAvTX0jYC+4GopQkCo6iimggC2pO41g77y84VwyhP6Ek+7PGZYDNgOawRZw==';
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$userinput=$username&$passwordinput=$password&$validation_input=$validation_input_value&$login=login&__EVENTVALIDATION=$event_valid&_VIEWSTATE=$view_state");
curl_setopt($ch, CURLOPT_USERAGENT, 'user-agent');
curl_exec ($ch); // execute the curl command
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
curl_setopt($ch, CURLOPT_URL, $finalUrl);
$buf2 = curl_exec ($ch);
curl_close ($ch);
print $buf2;
?>
It looks like you are missing two hidden fields that are in the source of that form. CSRF protection kinda. You can try scrapeing that, by doing a third request, before the other two, and getting those values, and putting them in the second request.
Also, like i said in my comment above, dont close your curl handler.
Any more info you can give would be awesome
EDIT:
As for the ASP page: asp is terribly difficult to curl this with. It could easily be hiding fields that you need. My suggestion would be to create a fake page, that print_r's $_POST and $_GET, and change the action of the form on their page with chrome, or firebug, to submit to your page. Just to check if you are missing anything
I did what i suggested to try, and i got this:
Array
(
[__EVENTTARGET] =>
[__EVENTARGUMENT] =>
[__VIEWSTATE] => /wEPDwUJLTQyMjg0NzI0D2QWAmYPZBYGAgEPZBYEAgYPFgIeB1Zpc2libGVoZAIHDxYCHwBoZAIDD2QWBAIBD2QWCAIBD2QWBAIBDw8WAh4EVGV4dGVkZAIFDw8WAh8AaGRkAgcPZBYCAgEPZBYCAgMPZBYCAgEPFgIfAGhkAgkPDxYCHwBoZGQCCw8PFgIfAGhkZAIDDxYCHwBoZAIFDw8WAh8BBXY8c2NyaXB0IGxhbmd1YWdlPSJqYXZhc2NyaXB0IiB0eXBlPSJ0ZXh0L2phdmFzY3JpcHQiPgokKGRvY3VtZW50KS5yZWFkeShmdW5jdGlvbigpIHsKVml0YWxTdGltLkluaXQoNCk7Cn0pOwo8L3NjcmlwdD4KZGRkdz/7+FcQ1E1sbC0Gua3jJsCGSnM=
[ctl00$ContentPlaceHolder1$uc_login$txtUser] => test
[ctl00$ContentPlaceHolder1$uc_login$txtPass] => test
[ctl00$ContentPlaceHolder1$uc_login$butLogin] => Login
[__EVENTVALIDATION] => /wEWBwKeiM4xAoi2mfkEAurz/r4MAvTX0jYC+4GopQkCo6iimggC2pO41g77y84VwyhP6Ek+7PGZYDNgOawRZw==
)
This ended up doing the trick.
http://www.mishainthecloud.com/2009/12/screen-scraping-aspnet-application-in.html?showComment=1368565341638#c9104469935977149435

Getting executed URL from CURL

I have a Affiliate URL Like http://track.abc.com/?affid=1234
open this link will go to http://www.abc.com
now i want to execute the http://track.abc.com/?affid=1234 Using CURL
and now how i can Get http://www.abc.com
with Curl ?
If you want cURL to follow redirect headers from the responses it receives, you need to set that option with:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
You may also want to limit the number of redirects it follows using:
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
So you'd using something similar to this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://track.abc.com/?affid=1234");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
Edit: Question wasn't exactly clear but from the comment below, if you want to get the redirect location, you need to get the headers from cURL and parse them for the Location header:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://track.abc.com/?affid=1234");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, true);
$data = curl_exec($ch);
This will give you the headers returned by the server in $data, simply parse through them to get the location header and you'll get your result. This question shows you how to do that.
I wrote a function that will extract any header from a cURL header response.
function getHeader($headerString, $key) {
preg_match('#\s\b' . $key . '\b:\s.*\s#', $headerString, $header);
return substr($header[0], strlen($key) + 3, -2);
}
In this case, you're looking for the value of the header Location. I tested the function by retrieving headers from a TinyURL, that redirects to http://google.se, using cURL.
$url = "http://tinyurl.com/dtrkv";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$location = getHeader($data, 'Location');
var_dump($location);
Output from the var_dump.
string(16) "http://google.se"

Categories