Download audio file using cUrl where authentication required to get file - php

I want to download a audio file from the server.
When I access the url direct on server that asks for a username & password I want to override it through cURL
Basically I want to allow my web users listen songs on my website. I have an authentication of other web to do so. currently users of my website have double check first they login in my website using credentials after that when they click on Play button then they enter again username & password that I am providing them (of the same web from i have authentication).
$url="https://example.com/abc.wav";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "userstring:passString");
curl_exec($ch);
curl_close($ch);
Direct URL after entering the username & password allowed me to listen the audio.
now how can I authenticate my web to access that URL by overriding authentication?
I want to download it and as well as play in the background after clicking on button "download" & "Play" respectively.
On the other hand is there any way that I can email that file without downloading or by downloading it

Use following code for authentication you forgot to set the header
header("Content-Type: audio/mpeg");
$url = "clip.mp3"
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "password");
curl_exec($ch);
header('Location: ' . $url);

Related

How can I use current site cookies to login to another site and then redirect to another page and make a CURL request?

How can I use current site cookies to login to another site and then redirect to another page ?
I have multiple websites which uses the same external Authenticator.I am trying to make a CURL request from one site to get data from the other site . But I have authentication issue while making the CURL request to other site.
In order to login to another site I need to click on login URL but credentials need not be entered again.
So My actual requirement is :
Use Cookies from current site.
Make a CURL request to another site login URL and login to that site using Cookies from current site.
Make a CURL request to another URL where you can obtain the required data.
I can obtain data from the other site if I am logged into that site in the browser.
My Current Code is as follows:
$agent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
$loginURL = $url . 'saml_login';
$cleanedURL = $url . 'anotherurl/list';
$curl = curl_init();
//Before making CURl request to $cleanedURL I need to make request to $loginURL(which has a redirect)
curl_setopt($curl, CURLOPT_URL, $cleanedURL);
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, TRUE);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Cookie: ' . $_SERVER['HTTP_COOKIE']));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($curl, CURLOPT_VERBOSE, TRUE);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
$response = curl_exec($curl);
var_dump($response);
All the examples I came across used either used credentials in the header or cookies file which is already exported.
Can any one help me to suggest it.
Cookie sharing is possible by domain. If your application at app.domain.com register a cookie for wildcard domain.com instead of app.domain.com, any subdomain.domain.com will have the access to the cookie key.

POWER BI Rest API Authentication using PHP

I am trying to authenticate using REST APIs, on my PHP backend. However, I am unable to obtain the auth code or access token.
I've registered my application on https://dev.powerbi.com/apps as a server side web-app, and have selected "Read and Write Datasets" permission on the
This is my code :
$url = "https://login.windows.net/common/oauth2/authorize"."?response_type=code"."&client_id=<my_client_id>"."&resource=https://analysis.windows.net/powerbi/api"."&redirect_uri=<my_redirect_url>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
curl_close($ch);
When I run this code, literally nothing happens. It doesn't redirect to my redirect URI.

Using CURL to download without a direct path | www.url.com/things?download=file

How does one download a file from a web page without a direct path to the file. For example a URL with GET information instead of the path. The code below seems to be downloading the actual page html instead of the file...
Not sure what I'm doing wrong. I also would like to augment this to also perform on sites that require logins but I think I would just have to add
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password")
to the code?
$output_filename = "advanced.exe";
$host = "http://download.cnet.com/Advanced-SystemCare-Free/3001-2086_4-10407614.html?hlndr=1";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_REFERER, "http://download.cnet.com");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
$fp = fopen($output_filename, 'w');
fwrite($fp, $result);
fclose($fp);
The link you have there isn't the actual link to the file, only the page that initiates the download. By the looks of it, the page uses JavaScript to trigger the download, so you would want to dig through their code to find out exactly how they do it. Then you can find the real URL to the file.
A simple way, if you are doing this only for one file, would be to download the file in your browser, and then access the URL it used from the browser's download manager. (In Firefox, for example, right click the file and choose "Copy Download Link")
I also would like to augment this to also perform on sites that require logins but I think I would just have to add ...
That would work only for HTTP based authentication. If the site uses a traditional login form, this will not work. You'd have to submit several, sequential HTTP requests via CURL, using cookies to store the session state.

PHP NTLM Authentication to Active Directory + keeping session

I've got NTLM (Active Directory) based service, and I need to write a PHP application. Normally, users are logging in to website with Activre Directory credentials, and it's ok.
But what I want to do, is to let them type in their credentials to PHP-written site, which in next step will use cURL to authenticate users to that Active Directory based site where they normally log in.
And this part is hard. I need then to keep session of users that through PHP cURL script authenticated to Active Directory based site in order to use them again later
(CRON querying site to determine that it has changed and automatically do some operations when this happens, which normally user has do manually).
In order to NOT store their credentials to authenticate again when this change happens, I somehow need to store NTLM session in PHP cURL site to every user that authenticated to
that site through this PHP cURL site.
My question is: Is that even possible?
Thanks in advance.
#Willem Mulder
The code you've posted actually does cookie-storing, but that is not my point becouse I've already done that (sorry for not writing it before). What I got so far is:
$cookie_file_path = dirname(__FILE__) . '/cookies.txt';
$ch = curl_init();
//==========================================================================
curl_setopt($ch, CURLOPT_USERPWD, $username. ':' . $password);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 100);
//==========================================================================
$ret = curl_exec($ch);
By using options CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR, cURL does the cookie storing in local file "cookies.txt". The problem is, that when I comment CURLOPT_USERPWD option (after authenticating and storing cookie, so theoretically I have session), I cannot authorize to website. Perhaps it reinitializes NTLM Handshake authorisation and is expecting username and password, which I don't want to store.
I want to store session info only, to provide service this session info and omit second authentication, but cURL seems to not take this data from cookie file, and REWRITES it with not relevant data send to me from service as response to NOT AUTHRORISED access request.
Well, yes you could
$ch = curl_init('http://www.google.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Get headers too with this line
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
// Get cookie
preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $m);
var_dump(parse_url($m[1]));
// And then of course store it somewhere :-)
As seen here how to get the cookies from a php curl into a variable

PHP - How to login via curl and download files?

Im using php, using curl Im accessing a directory on a server via http using httpauth. I pass it a username and password and it seems to work, but once I login how do I get the current directory and then proceed to download the files in that directory? Im using this curl code to authenticate:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
Just resend the credentials for any future requests. HTTP authentication doesn't keep track of logins; the client simply needs to resend the username / password pair for each request.

Categories