I have a page (input.php) that will allow a user to upload a CSV file. This file has 5 columns (SKU, Product, Quantity, Retail Price, and Total Retail Price). The CSV upload will only have the SKU number and Quantity filled in.
When the user hit upload the (import.php) page is suppose to go to the site and pull the product up by searching the SKU number and pulling the price and product (brand and title).
I paid a freelancher to create this code. I watched it work on his machine. I cant seem to get it to work on mine (wont pull price or product) and he is non-responsive now. Any help would be greatly appreciated!!
Edit: see notes for trouble shooting that I have already tried.
<?php
ini_set('max_execution_time', 0);
error_reporting(0);
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/". $_FILES["file"]["name"]);
$handle = fopen("upload/". $_FILES["file"]["name"], "r");
$file = '';
$line .= "SKU,Product,Quantity,Retail Price,Total Retail Price";
$file .= $line . PHP_EOL;
for ($i = 0; $row = fgetcsv($handle ); ++$i) {
// Do something will $row array
if($row[0]!="" AND $i>0)
{
$line="";
#echo "<pre>";
#print_r($row);
$SKU=$row[0];
$quantity=$row[2];
$loop=1;
do{
$url = "[www.homedepot.com];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($ch);
header("Content-Type:text/plain; charset=UTF-8");
$headers_arr = explode("\r\n", $headers);
$str=$headers_arr[5];
$arr=explode(":",$str);
$check=trim($arr[0]);
#echo $check; ### remove troubleshooting
if($check=="location")
{
#echo "Dustin"; ## remove troubleshooting
$productPageLink=$headers_arr[5];
$productPageLink=str_replace("Location:","",$productPageLink);
#echo $productPageLink; ## troubleshooting -- seems to be getting the links
$productPageLink=trim($productPageLink);
$productPageLink=str_replace("http:","https:",$productPageLink);
#echo $productPageLink; ## troubleshooting -- still seems to have links
$ch = curl_init();
#echo $ch; ##troubleshooting -- prints out "resouce id"
curl_setopt($ch, CURLOPT_URL, $productPageLink);
#echo $ch; ##troubleshooting -- prints out "resouce id"
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Language: en-US,en;q=0.5';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'Connection: keep-alive';
$headers[] = 'Te: Trailers';
#echo $headers; ##troubleshooting -- ## Troubleshooting -- prints out "Array"
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
#echo $result; ### troubleshooting - doesnt have any data
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch); # it end inside this if statement, however no error is printed
}
curl_close($ch);
preg_match_all('/<h2 class="product-title__brand" itemprop="brand" data-component="clickable brand link">(.*?)<\/h2>/s', $result, $output_array_brand);
#echo "<pre>"; ###
#print_r($output_array_brand);####
$brand=trim(strip_tags($output_array_brand[1][0]));
preg_match_all('/<h1 class="product-title__title">(.*)<\/h1>/', $result, $output_array);
$productTitle=$output_array[1][0];
$productTitle=$brand." ".$productTitle;
preg_match_all('/<span class="price__dollars">(.*?)<\/span>/s', $result, $output_array_price);
preg_match_all('/<span class="price__cents">(.*)<\/span>/', $result, $output_array_cent);
#echo "<pre>";
#print_r($output_array_price);
$price=trim(strip_tags($output_array_price[1][0]));
$cent=trim(strip_tags($output_array_cent[1][0]));
if($cent!="" OR $cent!=0)
{
$price=$price.".".$cent;
}
$line.=$row[0].",";
$line.='"'.$productTitle.'",';
$line.=$row[2].",";
$line.=$price.",";
$totalPrice=$row[2]*$price;
$line.=$totalPrice;
$file .= $line . PHP_EOL;
}
# echo "<br>";
$loop=$loop+1;
#echo "<br>";
if($loop>4)
{
if($check!="Location")
{
$line.=$row[0].",";
$line.=',';
$line.=$row[2].",";
$line.=",";
$line.="";
$file .= $line . PHP_EOL;
break;
}
}
}
while($check!="Location");
}
}
fclose($handle);
header('Content-Type: application/csv');
$output=$_REQUEST['output'];
header('Content-disposition: attachment; filename='.$output.'.csv');
echo $file;
#header('Content-disposition: attachment; filename='.$output.'1.csv');
#echo $file1;
exit;
?>
When the user hit upload the (import.php) page is suppose to go to the site and pull the product up by searching the SKU number and pulling the price and product (brand and title).
I paid a freelancher to create this code. I watched it work on his machine. I cant seem to get it to work on mine (wont pull price or product) and he is non-responsive now. Any help would be greatly appreciated!!
Edit: see notes for trouble shooting that I have already tried.
<?php
ini_set('max_execution_time', 0);
error_reporting(0);
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/". $_FILES["file"]["name"]);
$handle = fopen("upload/". $_FILES["file"]["name"], "r");
$file = '';
$line .= "SKU,Product,Quantity,Retail Price,Total Retail Price";
$file .= $line . PHP_EOL;
for ($i = 0; $row = fgetcsv($handle ); ++$i) {
// Do something will $row array
if($row[0]!="" AND $i>0)
{
$line="";
#echo "<pre>";
#print_r($row);
$SKU=$row[0];
$quantity=$row[2];
$loop=1;
do{
$url = "[www.homedepot.com];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($ch);
header("Content-Type:text/plain; charset=UTF-8");
$headers_arr = explode("\r\n", $headers);
$str=$headers_arr[5];
$arr=explode(":",$str);
$check=trim($arr[0]);
#echo $check; ### remove troubleshooting
if($check=="location")
{
#echo "Dustin"; ## remove troubleshooting
$productPageLink=$headers_arr[5];
$productPageLink=str_replace("Location:","",$productPageLink);
#echo $productPageLink; ## troubleshooting -- seems to be getting the links
$productPageLink=trim($productPageLink);
$productPageLink=str_replace("http:","https:",$productPageLink);
#echo $productPageLink; ## troubleshooting -- still seems to have links
$ch = curl_init();
#echo $ch; ##troubleshooting -- prints out "resouce id"
curl_setopt($ch, CURLOPT_URL, $productPageLink);
#echo $ch; ##troubleshooting -- prints out "resouce id"
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0';
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$headers[] = 'Accept-Language: en-US,en;q=0.5';
$headers[] = 'Upgrade-Insecure-Requests: 1';
$headers[] = 'Connection: keep-alive';
$headers[] = 'Te: Trailers';
#echo $headers; ##troubleshooting -- ## Troubleshooting -- prints out "Array"
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
#echo $result; ### troubleshooting - doesnt have any data
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch); # it end inside this if statement, however no error is printed
}
curl_close($ch);
preg_match_all('/<h2 class="product-title__brand" itemprop="brand" data-component="clickable brand link">(.*?)<\/h2>/s', $result, $output_array_brand);
#echo "<pre>"; ###
#print_r($output_array_brand);####
$brand=trim(strip_tags($output_array_brand[1][0]));
preg_match_all('/<h1 class="product-title__title">(.*)<\/h1>/', $result, $output_array);
$productTitle=$output_array[1][0];
$productTitle=$brand." ".$productTitle;
preg_match_all('/<span class="price__dollars">(.*?)<\/span>/s', $result, $output_array_price);
preg_match_all('/<span class="price__cents">(.*)<\/span>/', $result, $output_array_cent);
#echo "<pre>";
#print_r($output_array_price);
$price=trim(strip_tags($output_array_price[1][0]));
$cent=trim(strip_tags($output_array_cent[1][0]));
if($cent!="" OR $cent!=0)
{
$price=$price.".".$cent;
}
$line.=$row[0].",";
$line.='"'.$productTitle.'",';
$line.=$row[2].",";
$line.=$price.",";
$totalPrice=$row[2]*$price;
$line.=$totalPrice;
$file .= $line . PHP_EOL;
}
# echo "<br>";
$loop=$loop+1;
#echo "<br>";
if($loop>4)
{
if($check!="Location")
{
$line.=$row[0].",";
$line.=',';
$line.=$row[2].",";
$line.=",";
$line.="";
$file .= $line . PHP_EOL;
break;
}
}
}
while($check!="Location");
}
}
fclose($handle);
header('Content-Type: application/csv');
$output=$_REQUEST['output'];
header('Content-disposition: attachment; filename='.$output.'.csv');
echo $file;
#header('Content-disposition: attachment; filename='.$output.'1.csv');
#echo $file1;
exit;
?>