How to pass PHP_session_id in curl request - php

I am learning PHP and trying to automate a website login and then post some data to another page once logged in. I managed to login to the website and when I try to post data, I got a "Document moved".
Then I analysed the headers in firebug and realised that there was a PHP_session_id, so when I tried to manually pass this PHP_session_id it worked.
So my question is, how can I automatically get the sessionid when I login and then subsequently pass this on to my second request?
This is what my code looks like:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/login-main.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"loginType=company&username=johndoe%40gmail.com&password=1234&company=test");
ob_start();
curl_exec ($ch);
ob_end_clean();
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host:www.example.com',
'Origin:http://www.example.com',
'Cookie:PHPSESSID=na6ivnsdfab206ktb453o2au07',
'Referer:http://www.example.com/bookings/'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/bookings.php");
curl_setopt($ch, CURLOPT_POSTFIELDS,
"start_date=&end_date=&type=instructor&b_type=&id=41");
$buf2 = curl_exec ($ch);
curl_close ($ch);
echo "<PRE>".htmlentities($buf2);
?>

Add to your code in all curl requests
curl_setopt($ch, CURLOPT_COOKIEJAR, $full_path_to_cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $full_path_to_cookie_file);
For example
$full_path_to_cookie_file= __DIR__.'/cookie.txt';

Related

How to scrape data from Ajax or Json

I want to scrape data from this url
I am able to fetch simple data from html tags usign curl but not able to fetch data from Json or Ajax, I am not sure is it Ajax or Json data.
In below screen shot I want to fetch Appliance Models Data.
Which is coming form I think json or ajax. ==>>
Here below is my script to get data from page -
$loginURL = "https://www.apwagner.com/appliance-part/wpl/wp661600";
//$file='source.html'; //create a html file to save source code
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $loginURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
Please provide some guidance to fetch this info ..
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.apwagner.com/Product/GetPartModel");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"partNumber=wp661600&make=wpl");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
The part of data page gets through ajax request.
see this screenshot
You need to do it with curl
after your first curl response is received
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.apwagner.com/Product/GetPartModel");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"partNumber=wp661600&make=wpl");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
Or try to scrap data using python script
import string
import time
from selenium import webdriver
driver = webdriver.Chrome('<path to your chrome driver>')
driver.get('https://www.apwagner.com/appliance-part/wpl/wp661600');

Sending POST data to HTTPS redirect PHP

I need to redirect the current user to an external page (other domain) with some parameters that will be used on that page. To do that I'm using cURL, an example code is below:
$postfields = array('key'=>'value');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://domain.com/example.aspx');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);
curl_close ($ch);
But the user stays on the same page and I don't know if any data is transmitted.
What is the problem here? Is there another way to do that?

Login to on one page and then get another page passing all cookies from the first page along

I am trying to Get from my Api,Im using curl in php to log into on one page and
then get another page passing all cookies from the first page along with you but I keep getting Access denied!!
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://www.supersaas.com/api/users");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "?account=myname&password=mypassword");
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, "/tmp/cookieFileName");
curl_setopt($ch, CURLOPT_URL,"http://www.supersaas.com/api/users?id=12");
$buf2 = curl_exec ($ch);
curl_close ($ch);
echo "<PRE>".htmlentities($buf2);
What's wrong with this code????
Thanks
Your login didn't successful, hence you had invalid cookie in the file and got your second curl request denied from the server.
Remove the ? mark from the ?account. So it will be
curl_setopt($ch, CURLOPT_POSTFIELDS, "account=myname&password=mypassword");

Wifi Authorization using Curl

session_start();
function sendAuthorization($id, $minutes)
{
$unifiServer = "https://xxx.xxx.xxx.xxx:xxxx";
$unifiUser = "xxxxx";
$unifiPass = "xxxxx";
// Start Curl for login
$ch = curl_init();
// We are posting data
curl_setopt($ch, CURLOPT_POST, TRUE);
// Set up cookies
$cookie_file = "/tmp/unifi_cookie";
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
// Allow Self Signed Certs
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// Force SSL3 only
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
// Login to the UniFi controller
curl_setopt($ch, CURLOPT_URL, "$unifiServer/login");
curl_setopt($ch, CURLOPT_POSTFIELDS,
"login=login&username=$unifiUser&password=$unifiPass");
// send login command
curl_exec ($ch);
// Send user to authorize and the time allowed
$data = json_encode(array(
'cmd'=>'authorize-guest',
'mac'=>$id,
'minutes'=> $minutes));
// Send the command to the API
curl_setopt($ch, CURLOPT_URL, $unifiServer.'/api/cmd/stamgr');
curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='.$data);
curl_exec ($ch);
// Logout of the UniFi Controller
curl_setopt($ch, CURLOPT_URL, $unifiServer.'/logout');
curl_exec ($ch);
curl_close ($ch);
unset($ch);
}
(thought I would add the code first)
would anyone out there know how to turn this around so that I could call the function so the user IS NOT authorized?
Im making a captive portal. this is a authorized.php page, but on this i would like to have a function which is unauthorize within this page, to call it if some information is missing.
Thanks,
Andrew

AS3 do post through PHP without changing doted parameters

I am trying to communicate with Google Forms using AS3 and PHP. The problem is that when I pass parameters such 'entry.283123610', PHP changes these into 'entry_283123610'. How can I prevent PHP from changing the post variables? This is the code I am currently using:
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($resource, CURLOPT_POST, 1);
curl_setopt($resource, CURLOPT_POSTFIELDS, $params);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();

Categories