How to display content of other page which has authentication enabled?
Warning: file_get_contents(http://abc/report.cmd?report=123): failed to open stream: HTTP request failed! HTTP/1.1 401 Authorization Required in //html/tracker/index2.php on line 13
$file_path = 'http://abc/report.cmd?report=123';
$content=file_get_contents($file_path);
echo $content;
Use the curl library to authenticate
<?php
// create a new cURL resource
$curl = curl_init();
// set URL and other appropriate options
curl_setopt($curl, CURLOPT_URL, "http://www.example.com/");
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
// grab URL and pass it to the browser
curl_exec($curl);
// close cURL resource, and free up system resources
curl_close($curl);
?>
Authenticate first.., then request the request the content.
Here's an example of authenticating against HTTP Basic auth w/ PHP:
http://php.net/manual/en/features.http-auth.php
Related
I'm using the restserver in Codeigniter from Chris Kacerguis and I setup rest_auth with session.
When I'm logged in, I access the page whose view.php get the response through file_get_contents and it's returning 401 Unauthorized.
When I access the API's direct page in a new tab, it returns the json correctly.
the view.php is loading the API content in this way:
json_decode(file_get_contents('https://example.com/api/proces/'), true);
the response:
A PHP Error was encountered
Severity: Warning
Message: file_get_contents( I needed to remove the link ): failed to open >stream: HTTP request failed! HTTP/1.1 401 Unauthorized
What's happening?
I see your example url is using "https" and it is possible that your Apache / Nginx server is not configured correctly to handle the SSL in this way, but it would work fine with the certificate in the browser.
I would suggest you use curl as an alternative instead.
json_decode(curl_get_contents('https://example.com/api/process/'));
from here https://github.com/zg/curl_get_contents
Or of course you could use curl directly.
$url = 'https://example.com/api/process/';
$request = curl_init();
curl_setopt($request, CURLOPT_URL, $url);
curl_setopt($request, CURLOPT_RETURNTRANSFER, true);
curl_setopt($request, CURLOPT_HEADER, false);
$data = curl_exec($request);
curl_close($request);
I written simple php code to get some url content but it dosn't work
it return this error
file_get_contents(http://www.nature.com/nature/journal/v508/n7496/full/nature13001.html) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 401 Unauthorized
here is my code. please help me.tnx
$content=file_get_contents('http://www.nature.com/nature/journal/v508/n7496/full/nature13001.html');
echo $content;
Here's an alternative to file_get_contents using cURL:
$url = 'http://www.example.com';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
You might want to add this curl_setopt($curl, CURLOPT_ENCODING ,""); if you encounter encoding problem.
Open the page using your browser and with the console open, see that the server does indeed send a 401 even when page is sent and viewable
On php, open the url in an alternate way to ignore the error (see http://php.net/manual/en/context.http.php)
You'll also notice that it's gzip-encoded, see http://php.net/manual/en/function.gzinflate.php
Happy hacking!
I am using KODAK api to integrate photo print functionality in my website. All is working fine but it gives error message at the time of creating order as you can see on below URL
https://ws.test.kdfse.com/vasp2/rest/order/?retailer=Target&app=PNMEMORYDEMO&country=US&app_version=1.0
If you are not able to see the error message, you can see message which I have got as error
------------------------------
HTTP Status 401
type : Status report
message:Method Not Allowed
description : The specified HTTP method is not allowed for the requested resource (Method Not Allowed).
GlassFish Server Open Source Edition 3.1.2.2
------------------------------
For that reason, I have also talked with kodak api technical support but they said that make sure to set the correct "Request method" as POST and "Content-Type" as application/x-www-form-urlencoded when making this call.
I have used cURL for making this call but doesn't know how to set request type & content type in CURL request.
I am using below code. Please suggest & correct me for the same.
$ch = curl_init("https://ws.test.kdfse.com/vasp2/rest/order/PNPlasqTouch_T1366049430039_OKS13105x64222081_CTARGET/status/current");
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_TIMEOUT,60);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$result=curl_exec ($ch);
echo "<pre>";print_r($result);exit;
Thanks in advance.
By HTTP Status 401 it means you are unauthorized. I have clicked your link and it asked for user/password. So set the authentication as follows:
// use your user/password at here
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
You can use;
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
And final code will be;
$ch = curl_init("https://ws.test.kdfse.com/vasp2/rest/order/PNPlasqTouch_T1366049430039_OKS13105x64222081_CTARGET/status/current");
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_TIMEOUT,60);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl, CURLOPT_USERPWD, "your_username:your_password");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$result=curl_exec ($ch);
echo "<pre>";print_r($result);exit;
Update: You can see here for more detail about setting http headers. Also you are using secured service, you may set CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST as 1
Is there a configuration/setting for the Wowza server that must be correct to allow HTTP Authentication?
According to How to get realtime connection counts from Wowza Media Server, I should be able to get some XML information that I need. If I go to the url from a browser, it prompts for username/password, and I get the data, but if I try to accomplish it from PHP it fails no matter what method I use:
HTTP request failed! HTTP/1.0 401 Unauthorized
Method #1:
$data = file_get_contents('http://admin:password#123.123.123.123:8086/connectioncounts');
Method #2:
$handle = fopen('http://admin:password#123.123.123.123:8086/connectioncounts', "r")
Method #3:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, http://123.123.123.123:8086/connectioncounts);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, "admin:password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$output = curl_exec($ch);
Try change CURLAUTH_BASIC to CURLAUTH_ANY
I want to access an xml file from https://test24.highrisehq.com/tasks/upcoming.xml using php.
Following is the sample code:
$xml = simplexml_load_file("https://test24.highrisehq.com/tasks/upcoming.xml");
since the connection is secured I am getting an error:
Warning:
simplexml_load_file(https://test24.highrisehq.com/tasks/upcoming.xml)
[function.simplexml-load-file]: failed
to open stream: HTTP request failed!
HTTP/1.1 401 Unauthorized in
D:\wamp\www\xml\index.php on line 3
I need to put in the username and password somewhere in the code before accessing the file. Can anyone please tell he how can I achieve this ?
Regards
Umair
cURL, you seek cURL! (Seriously, cURL is awesome. Scroll down at that link and you'll find the HTTP Authentication example below)
// HTTP authentication
$url = "https://www.example.com/protected/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "myusername:mypassword");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //see link below
$result = curl_exec($ch);
curl_close($ch);
echo $result;
You can find information about using CURLOPT_SSL_VERIFYPEER here.