Hi, this is my first post on here so go easy 😉
I got the following script from the main php site but been a noob I'm not doing too well. Ideally I want to log in to the site which sits behind basic IIS athentication ( user ID & PW ) and either grab all the files I need in one go.... around 20 and copy to a specific dir on my own site and grab each one and pull the data in to SQL. I started by just trying to get access and copy the files in the first place. Eventually I want to get all the data in to a MySQL DB which I will be using a C# application with.
So far I have not been able to even get one file transfered over and now I am gettin server 500 problems. Because of this and a lack of debug skills ( and tbh noobness ) I'm not sure where its even going wrong.
Can any one help me ?
<?php
/* Simple PHP Script to login on a Basic Authentication page. */
/* Access Configuration */
define ('x401_host', 'EDITED');
define ('x401_port', '80');
define ('x401_user', 'EDITED');
define ('x401_pass', 'EDITED);
/* Function */
function get401Page($file)
{
$out = "GET $file HTTP/1.1\r\n";
$out .= "Host: ".x401_host."t\r\n";
$out .= "Connection: Close\r\n";
$out .= "Authorization: Basic ".base64_encode(x401_user.":".x401_pass)."\r\n";
$out .= "\r\n";
if (!$conex = @fsockopen(x401_host, x401_port, $errno, $errstr, 10)) return 0;
fwrite($conex, $out);
$data = '';
while (!feof($conex));
{
$data .= fgets($conex, 512);
}
fclose($conex);
return $data;
}
/* Code */
if ($source = get401Page('/absolute/path/file.php?get=value'))
{
echo $source;
}
else;
{
echo "I can't connect!";
}
?>
I have also tried to use curl but again this is going wrong
<?php
// http://eugeneciurana.com/~zhenya01/books/webprog/pcook/ch17_07.htm
// http://www.sda-asia.com/php_information/psecom,id,4,articles,550.html
$local = "/England/UnplannedEvent/metadata.xml";
$remote = "http://EDITED/England/UnplannedEvent/metadata.xml";
$id = "EDITED";
$password = "EDITED";
$loginURL = "EDITED.com";
$fh = fopen($local, 'w') or die($php_errormsg);
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$POSTFIELDS = 'UserName='.$id.'&hiddenVar1=hiddenVar1Value&UserPass='.$password;
$ch = curl_init();
curl_setopt($ch, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD,"$id:$password");
curl_setopt($ch, curlOPT_URL,$loginURL);
curl_setopt($ch, curlOPT_SSL_VERIFYHOST,2);
curl_setopt($ch, curlOPT_USERAGENT, $agent);
curl_setopt($ch, curlOPT_RETURNTRANSFER,1);
curl_setopt($ch, curlOPT_FOLLOWLOCATION,1);
curl_setopt($ch, curlOPT_SSL_VERIFYPEER,FALSE); // this line makes it work under https
$result=curl_exec ($ch);
// SET FILE TO DOWNLOAD
// $local is the location to store file on local machine
curl_setopt($ch, CURLOPT_URL, '$remote' CURLOPT_FILE, $fh);
// EXECUTE REQUEST (FILE DOWNLOAD)
$content = curl_exec ($ch);
curl_close ($ch);
echo("Results: <br>".$result);
?>
Many Thanks In Advance
Terran