Searching for specific string in the contents of a call using curl
I am using CURL to to get information from a form submission. I am then
taking the string of data returned searching for the word "Error" and
based on whether it is found redirecting and logging the error into a txt
file. Everything works fine except I need to get the error number in the
string. To do that I am trying to find the word "Error" and then grabbing
and numbers that come after it. However something is wrong as nothing is
printing for the error number. When I try to output the results of the
pregmatch used I just get NULL. My code is below.
$ch='';
$Rec_Data='';
$Temp_Output='';
if($_SERVER['REQUEST_METHOD']==='POST') { // REQUIRE POST OR DIE
if(isset($_POST['lname'])) $lname=$_POST['lname']; // GET lastname INTO VAR
if(isset($_POST['fname'])) $fname=$_POST['fname']; // GET firstname INTO VAR
if(isset($_POST['uid'])) $uid=$_POST['uid']; // GET uid INTO VAR
if(isset($_POST['rsp'])) $rsp=$_POST['rsp']; // GET rsp INTO VAR
if(isset($_POST['z1'])) $z1=$_POST['z1']; // GET z1 INTO VAR
if(isset($_POST['module'])) $module=$_POST['module']; // GET module INTO VAR
if(isset($_POST['CFID'])) $CFID=$_POST['CFID']; // GET CFID INTO VAR
if(isset($_POST['CFTOKEN'])) $CFTOKEN=$_POST['CFTOKEN']; // GET CFTOKEN
INTO VAR
$PostVars = "lname=" . $lname . "&fname=". $fname . "&uid=" . $uid .
"&rsp=" . $rsp . "&z1=" . $z1 . "&module=" . $module . "&CFID=" .
$CFID . "&CFTOKEN=" . $CFTOKEN;
$ch = curl_init(POSTURL);
curl_setopt($ch, CURLOPT_POST ,1);
curl_setopt($ch, CURLOPT_POSTFIELDS , $PostVars);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER ,0); // DO NOT RETURN HTTP HEADERS
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,1); // RETURN THE CONTENTS OF
THE CALL
$Rec_Data = curl_exec($ch);
ob_start();
header("Content-Type: text/html");
$Temp_Output = $Rec_Data;
if(strpos($Temp_Output,"Error")>=0){
preg_match("/Error (\d+)/", $Temp_Output, $error);
var_dump ($error[1]);
$date = date('Y/m/d H:i:s');
//log error
$logFile = "error_log.txt";
$fh = fopen($logFile, 'a') or die("can't find open file");
$errorString = "UID = ".$CFID." - Error# = ".$error[0]." - Date =
".$date." \n";
fwrite($fh, $errorString);
fclose($fh);
if(strpos($Temp_Output,"Error 2")>=0){
//we have an error2! User in system for some strange reason.
//do stuff here.
//Give them their cert.
//log this user in "stupid errors" table
//table consists of uid, error, date/time
header("Location:http://abim.improvementskills.org/content/certificate.cfm?CFID=".$CFID."&CFTOKEN=".$CFTOKEN."");
}
}
else{ //no error. give them their cert
header("Location:http://abim.improvementskills.org/content/certificate.cfm?CFID=".$CFID."&CFTOKEN=".$CFTOKEN."");
}
curl_close($ch);
} else die('Hacking attempt Logged!');
exit;
No comments:
Post a Comment