PHP: Download file from different web server using basic authentication?
-
19-09-2019 - |
Question
I have been using PHP for a while but I am not too too advanced. I do not have much experience with server-to-server stuff. I need to research setting up a data-feed with a vendor. The contact person that I talked to said I would be downloading a file from their server using a username and password via basic authentication. Can someone please give me a break down on how to do that?
Thanks!!
Solution
$f = fopen("http://$username:$password@example.com");
then just use the same methods as for manipulating files, eg. fread, fgets, fclose, etc.
OTHER TIPS
Consists in placing this request header in the HTTP request:
$login = "havenard";
$pass = "my l33t p4ssw0rd";
$header = "Authorization: Basic " . base64_encode("{$login}:{$pass}");
Now how you're going to put it there will depend on the methods you are using to make this connection.
I think that if they told you to "download a file", you might be looking to implement a cURL based solution: PHP: cURL - Manual
And you can use the header request that Havenard listed in his post:
$login = "havenard";
$pass = "my l33t p4ssw0rd";
$auth = "Authorization: Basic " . base64_encode("{$login}:{$pass}");
They probably mean using a SOAP API, this format uses (usually) a username/password + function call to get / post data from the service. Do a search on php SOAP, or nusoap, etc. That should give you enough to get started.
I would however get a solid 'name' of the service to use. I am assuming here for you that its SOAP as that is what I am most familiar with from personal usage.
Edit: Unless by 'basic authentication' they mean: http://us.php.net/manual/en/features.http-auth.php
I assume that the person means something like HTTPAuth, which is when a username/password box pops up when going to a page in your browser, and you're required to give credentials before the content is loaded.
For this, you can use CURL. PHP has a nice set of curl functions for handling this. You'll need to configure it with the CURLOPT_NETRC option.