I previously found a script while googling and used it for scrapping purpose, my main class
in my amazon.php, I wrote the following script
include('scrape.php');
set_time_limit(0);
$ASIN = 'B000GEM3RI';
$shipArray = shipingPrice($ASIN);
var_dump($shipArray);
print_r($shipArray);
echo $shipArray;
function shipingPrice($city){
$shipArray = array();
$scrape = new Scrape();
$url = 'http://www.amazon.com/gp/offer-listing/'.$city.'/ref=dp_olp_new?ie=UTF8&condition=new';
$scrape->fetch($url);
$data = $scrape->removeNewlines($scrape->result);
$data = $scrape->fetchBetween('<table cellspacing="0" cellpadding="0" width="100%" border="0"> <thead class="columnheader"><tr><th scope="col" class="price">Price + Shipping</th><th scope="col" class="condition">Condition</th><th scope="col" class="seller">Seller Information</th><th scope="col" class="readytobuy">Buying Options</th></tr></thead>','</table>',$data,true);
$rows = $scrape->fetchAllBetween('<tr','</tr>',$data,true);
$i=0;$j=0;
foreach ($rows as $row){
if($i!=0){
if($i!=2){
$record = array();
$cells = $scrape->fetchAllBetween('<td','</td>',$row,true);
$record['price'] = strip_tags($cells[0]);
if(stristr($record['price'],'oz')===False && stristr($record['price'],'/')===False)
{
$listPrice=$scrape->fetchBetween('$',' +',$record['price']);
}else{
$listPrice=$scrape->fetchBetween('$',' (',$record['price']);
}
//print_r($listPrice);
if($listPrice==''){
$listPrice=$scrape->fetchBetween('$',' &',$record['price']);
$shipPrice='0';
}else{
$shipPrice=$scrape->fetchBetween('+ $','s',$record['price']);
}
$shipPrice= floatval($shipPrice);
//####
$sellerIdInfo = $cells[2]; $sellerIdArray=$scrape->fetchAllBetween('&marketplaceSeller=0&seller=','"><b>',$sellerIdInfo);
if(count($sellerIdArray)>1){
$sellerId=$sellerIdArray[0];
}else{
$temp = explode('"id',$sellerIdArray[0]);
$sellerId=$temp[0];
}
//##
$sellerName =$scrape->fetchBetween('Seller:','Seller',$record['price']);
$sellerInfo=$scrape->fetchAllBetween('alt="','"',$cells[2],true);
$sellerName=str_replace(array('alt="','"'),array('',''),$sellerInfo[0]);
if($sellerName!=""){
//
}else{
$sellerName = $scrape->fetchBetween('<span class="sellerHeader">Seller:</span>','</b></a>',$cells[2],true);
$sellerName=str_replace("Seller:","",$sellerName);
$sellerName=$scrape->fetchBetween('<b>','</b>',$sellerName);
}
array_push($shipArray,array('sellerName'=>$sellerName,'sellerId'=>$sellerId,'price'=>$listPrice,'shipPrice'=>$shipPrice));
}
}
$i++;
}
return $shipArray;
}
the url for this scrip is amazon
when I echo it, var_dump it or print_r, an empty array is displayed, I checked the page using firebug and to me, looks like everything is okay in my code
can somebody tell me why I can access anything from the page although my code is okay?
thanks for helping me
EDIT:-
By adding return $this->result = curl_exec($ch); in my scrap class function fetch($url), I have assured that Page is being retrieved successfullly…
EDIT-2:-
after working on the advice as provided in answer, it tried
$shipArray[]=array('sellerName'=>$sellerName,'sellerId'=>$sellerId,'price'=>$listPrice,'shipPrice'=>$shipPrice);
in my function, but still the same empty array
EDIT-3
I changed the following function
function fetchBetween($needle1,$needle2,$haystack,$include=false){
$position = strpos($haystack,$needle1);
if ($position === false) { return ' it was null data'; }
when I echo echo $data; in my script file,
it was null data
is printed, so looks like this line of code $position = strpos($haystack,$needle1); is not working,
am I right?
if yes, what to do now?
Gotcha, I was correct, issue was not with my code
on line
there was an extra space
border="0"> <thead class, that was creating issue, once, I removed this the space frommy code became okay..
thanks to everyone