Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 770
Omayr Avatar asked Nov 21 '25 14:11

Omayr


1 Answers

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.

like image 186
cwallenpoole Avatar answered Nov 23 '25 04:11

cwallenpoole