cURL - require cookies and session? - php

I am trying to log in a website using php via cURL. But the website I am trying to log in requires cookies and session I am new to php what is the best way to set up cookies and session so that I can log in I have tried $_SESSION and setcookies they don't seem to work.. here is the code I have written any help on this topic would be helpful thanks
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://example.com/logint");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'username' => 'username',
'passwd' => 'pass',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
echo($output);
curl_close($ch);
?>

To login to a webpage using curl and get the cookie you do something like this:
curl -c cookies.txt -d "username=myUser&password=FooBar" http://xyz.com/login
and to use those cookie in later requests you do:
curl -b cookies.txt http://whatever.com/getPage
P.S: Not sure abt PHP syntax

Related

Is there a way of sending/receiving SMS(s) from a Huawei B315 LTE Router with PHP?

I tried to access the router's interface with curl but I am a new coder and I struggled to make this happen.
<?php
$data = array( "User name" => "admin", "password" => "*****", "form" => "submit" );
// Initialized the cURL session with Target URL
$ch = curl_init("192.168.8.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, "true"); // Allow redirections
curl_setopt($ch, CURLOPT_POST, true); // We are making a post request
/*curl_setopt($ch, CURLOPT_POSTFIELDS, $data);*/
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, "true");
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
I know this answer is a few years late, but for anyone still searching for answers to this, there is a PHP script on Github that allows for sending and receiving SMS via the Huawei-B315s-22 HTTP API. See https://github.com/Zili0/Huawei-B315s-22
I noticed that it calls the following endpoints:
/api/webserver/SesTokInfo
/api/user/state-login
/api/monitoring/status
/api/user/authentication_login
/api/device/information
/api/net/current-plmn
/api/monitoring/traffic-statistics
/api/sms/sms-count
/api/sms/sms-list
/api/wlan/multi-basic-settings
/api/device/control
/api/user/logout
There's also two other similar scripts I found, both written in Python that do similar things. Hopefully one of these will provide the kind of thing you need:
https://github.com/shaan1337/huawei_b315s22_sms
https://github.com/chenwei791129/Huawei-LTE-Router-SMS-to-E-mail-Sender

Why curl doesn't work with my active session?

I have issue with my feature which I want add to my website.
When I'm trying exec curl, script getting crash (HTTP ERROR 500).
There is something what I don't know about session and curl?
Everything works without session OR withour execute curl.
How I can fix that or where I should look to issue?
<?php
session_start();
include("include/function.php");
if($_SESSION['logadm']=="adm" or $_SESSION['logmod']=="mod")
{
$link = $_GET['link'];
$curl = curl_init("https://discordapp.com/api/webhooks/keyyy");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode(array("content" => "$link", "username" => "webhook")));
curl_exec($curl)
}
header("Location: ".$_SERVER['HTTP_REFERER']."");
?>

Auth lost when doing a curl request to my laravel server

I'm trying to make a curl request to my laravel server, in that request I have to check whether the user of my laravel application is logged in or not. I use this code:
$transferAmount = 200;
//set POST variables
$url = URL::route('post-spend-partner');
$fields = array(
'transferAmount' => urlencode($transferAmount),
'cancelUrl' => urlencode(URL::route('get-return-page-example')),
'returnUrl' => urlencode(URL::route('get-return-page-example')),
);
// New Connection
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
In the requested url I'm just checking if I'm logged in or not, but it always returns false:
public function postSpendPartner() {
echo "Authenticated? " . (Auth::check() ? 'Yes' : 'No');
}
I know for sure that I'm logged in, if I try the exact same thing with Ajax it completely works!
Does anyone know what I could try, to solve this problem?
Best regards!
Fabrice
Some facts: HTTP is stateless. Session IDs need to be passed to the server in order to continue the session. Session IDs are (most of the time) stored in cookies. Cookies are included in the request.
Using a cookiejar could indeed be one possible solution. The fact that it works using Ajax, and not by re-submitting the request from your server might be because of the session-verification mechanism on the server: Some session implementations lock session IDs to the initial IP address. If the contents of your cookiejar file check out, that might be the culprit.
That aside: re-submitting the request via Curl from your server is a severe codesmell to me. A proper solution would to implement something such as OAuth.
Try sending your cookies as a header with your curl request.
// ...
$cookie_header = "Cookie:";
$headers = [];
foreach($_COOKIE as $key => $val) {
// Do sanitize cookie values
$cookie_header .= " ".$key."=".$value.";";
}
$headers[] = $cookie_header;
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
// ...
You could filter out unnecessary cookie values from $cookie_header.

login to dropbox with curl ( user login )

Am trying to log in to Dropbox as a user using cURL and PHP.
$ch = curl_init();
$data = array(
't'=>'hxdlvCcN7SKKcfKCvpEO8-s2',
'lhs_type'=>'anywhere',
'login_email'=>'myemail#mail.com',
'password'=>'mypass',
'login_submit'=>1,
'remember_me'=>'on',
'login_submit_dummy'=>'Sign in'
);
// set cURL options and execute
curl_setopt($ch, CURLOPT_URL, "https://www.dropbox.com/login?lhs_type=anywhere");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$request_token_response = curl_exec($ch);
var_dump($request_token_response );
I get 403 page with this text:
It seems you tried to do something we can't verify. Did you log into a different Dropbox account in a different window? Try clicking here to go back to the page you came from, or just go home.
What am I doing wrong?
It is possible, and there's a current class available to make it easy. https://github.com/jakajancar/DropboxUploader/
It's as easy as
require 'DropboxUploader.php';
$uploader = new DropboxUploader('email#address.com', 'password');
The $uploader->loggedin will return if you are logged in or not.
I think you'll find that the 't' value can only be used once. You need to call for a fresh one each time you go to log in.
If you look at the class suggested in the other post, you will see that it does just that.

PHP-cURL --> follow a ruby redirection

I am using trying to extract the wiki documentation from a page on my website (redmine). I successfully retrieved the redirect to the login page, send post username and password field, but then I arrive on a page with a ruby redirect from redmine and I can't go further. The ruby code in the file "response.rb" of redmine is:
def redirect(url, status)
self.status = status
self.location = url.gsub(/[\r\n]/, '')
self.body = "<html><body>You are being redirected.</body></html>"
end
I don't kbow enough about ruby to modify redmine's code, so is there a way to do this in php?
curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, true);
Would tell cURL to follow any properly configured redirects (anything that sends the Location: header).
Found an alternate solution!
IF anybody else has that problem with redmine or any other redirect:
Use cURL to login first
Save your session to a cookie
Use cURL to get your secured page that needs authentification.
<?php
$tempFilename = tempnam("/tmp", "COOKIE");
// Login
$curlHandle = curl_init("http://bob.com/login");
curl_setopt($curlHandle, CURLOPT_COOKIEJAR, $tempFilename);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$dataArray = array(
"username" => "bob",
"password" => "password"
);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $dataArray);
$output = curl_exec($curlHandle);
// Getting the wiki
$curlHandle = curl_init("http://bob.com/wiki");
curl_setopt($curlHandle, CURLOPT_COOKIEFILE, $tempFilename);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($curlHandle);
echo $output;
?>

Categories