HTTP Authentication fails from PHP script on Wowza Server? - php

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

Related

Response received from a url via php script is smaller than when I open it in browser

I'm trying to fetch this url via a script: http://api.alarabiya.net/sections/2/
But JSON response received is much smaller than when I open it directly in a browser,
please notice that I tried this url through CURL and set the same USER-AGENT of the browser and all request header used in the browser and I still get a smaller response.
Here's an exmaple using just file_get_contents
<?php
echo file_get_contents("http://api.alarabiya.net/sections/2/");
?>
My question is if there's a request size limit when using file_get_contents or if the PHP's memory can't handle it or what's the problem exactly?
When I CURLed this in shell it gave me the same o/p as in php (the trimmed output).
I finally found a solution for this:
$url = "http://api.alarabiya.net/sections/2/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true );
// This is what solved the issue (Accepting gzip encoding)
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
$response = curl_exec($ch);
curl_close($ch);
echo $response;

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.

Display of another authentication enabled page content?

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

how to authenticate username/password of https site to access a file in php

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.

Using PHP Curl, how can I look at exactly what is being sent?

For instance, on the following CURL snippet:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); //set target URL
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);// allow redirects
curl_setopt($ch, CURLOPT_POST, $usePost); // set POST method
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); //set headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, $returnHeaders);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //prevent unverified SSL error
Before I run curl_exec on it, what if I want to see the full request headers and body before it is sent. (to see if is correctly following certain REST API guidelines)
You could send a request to the local server:
$test_url = 'http://localhost/nonexistent-page';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $test_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
// Other options.
curl_exec($ch);
echo nl2br(curl_getinfo($ch, CURLINFO_HEADER_OUT));
This will give you the request headers, with only the request line path and the Host: line being different from your actual request.
If you have access to a graphical environment on your server, you could use Wireshark to examine the network packets being sent and received. Wireshark allows you to use filters, to filter out specific IP-adresses and protocols.
For instance, I use this filter to see all the traffic from my cURL requests/responses to the server with IP w.x.y.z (substitute with the ip of the server you are connecting to):
ip.addr == w.x.y.z && http
I can then examine all my requests responses.
This has given me great insight in what's happening 'under the hood'.

Categories