Welcome to WebmasterWorld Guest from 3.227.249.234

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

php script help

     
7:10 pm on Dec 22, 2004 (gmt 0)

Full Member

10+ Year Member

joined:June 16, 2004
posts:232
votes: 0


yeah hi, ok haveing problems with the php script, i mean the script works however i am having trouble modding it a bit. the thing is that i need the modifity the $end variable of the script, instead of looking for a unique word i need to a number of lines or words. The thing is i just need the number preciding the List Price:?...ummm..yeah thats about it. THanks

<?php

// include trailing slash in file
$url_to_grab = "http://www.amazon.com";

//IMPORTANT: BOTH OF THESE MUST BE UNIQUE PIECES OF CODE FROM
//THE TARGET WEBSITE--IF BOTH OF THESE ARE NOT UNIQUE,
//THIS SCRIPT WILL NOT WORK!
$start="List Price:</td>";
$end="";


// Grab source code from a file or web site
if(!($myFile=@fopen($url_to_grab,"r"))) {
echo "Unable to get information from $url_to_grab.";
exit;
}

while(!feof($myFile)) {
// Read each line and add to $myLine
$myLine.=fgets($myFile,255);
}
fclose($myFile);

$start_position=strpos($myLine, $start);
$end_position=strpos($myLine, $end)+strlen($end);
$length=$end_position-$start_position;
$myContent=substr($myLine, $start_position, $length);

echo $myContent;

?>

10:27 pm on Dec 22, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1477
votes: 0


Of course, $myContent includes everything between and including $start and $end, so you just need to figure out where $end is.

What is your goal, here? Are you hoping to scour am****.com for list prices and return them? Could you post a tiny bit of code that illustrates the section of the file you are attempting to read and the data you want to receive?

4:50 am on Dec 23, 2004 (gmt 0)

Full Member

10+ Year Member

joined:June 16, 2004
posts:232
votes: 0


umm..say for exaplme i am looking for to the list price for this book [amazon.com]

i know how to get the start location
$start=>List Price:</td>
but cannnot figure out the end. Already tried "<td>" and various other combinations, the thing is the end variable has to be unique, so i was thinking why not just take the next few letter that come after "List Price:</td>"

The Script Source is something like this

<td class="productLabel">List Price:</td>
<td>$30.00</td>

[edited by: jatar_k at 7:34 pm (utc) on Dec. 27, 2004]
[edit reason] fixed sidescroll [/edit]

5:56 am on Dec 23, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:Nov 21, 2004
posts:62
votes: 0


Try something like this for the last bit of the script:

$start_position=strpos($myLine, $start)+strlen($start);
$substring=substr($myline, $start_position);
$end_position=strpos($substring, $end);
$myContent=substr($myLine, 0, $end_position);

echo $myContent;

---------
$start will be Like you had it. And $end can now be '</td>' or whatever.

The change will make a new string starting from the position where $start ended. And will search from that point onwards for whatever you have in $end. It will then extract the string up to in front of that point.

So on the page you gave as an example, it'll extract '$30.00'

No, I didn't test it. ;-)

-V

5:16 pm on Dec 23, 2004 (gmt 0)

Full Member

10+ Year Member

joined:June 16, 2004
posts:232
votes: 0


thanks for the input veroxii but i cant still get it to work, the script runs without any errors but just shows a blank page.

<?php
$url_to_grab = "http://www.amazon.com/exec/obidos/tg/detail/-/0534492584/";

$start="List Price:</td>";
$end="</td>";

if(!($myFile=@fopen($url_to_grab,"r"))) {
echo "Unable to get information from $url_to_grab.";
exit;
}

while(!feof($myFile)) {
$myLine.=fgets($myFile,255);
}
fclose($myFile);

$start_position=strpos($myLine, $start)+strlen($start);
$substring=substr($myline, $start_position);
$end_position=strpos($substring, $end);
$myContent=substr($myLine, 0, $end_position);

echo $myContent;

?>

[edited by: jatar_k at 7:35 pm (utc) on Dec. 27, 2004]
[edit reason] shortened url [/edit]

6:05 pm on Dec 23, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 20, 2004
posts:1477
votes: 0


<edit>WARNING: The methods used and the databases that contain the Ama***.com list price info may be owned/copyrighted/patented by them, and you probably need to gain their permission before using this in your own application.</edit>

<?php
$url_to_grab = "[some ama***.com query, as above]";

if(!($myFile=@fopen($url_to_grab,"r"))) {
echo "Unable to get information from $url_to_grab.";
exit;
}

while(!feof($myFile)) {
$myLine.=fgets($myFile,255);
}
fclose($myFile);

# THIS CODE DIRECTLY PRECEDES THE FIRST LIST PRICE DISPLAY
# I ADDED 18 TO $start_position TO KEEP THE VARIABLE FROM DISPLAYING, TOO
# CHANGE TO class=\"price\" AND + 14 TO GRAB THE FIRST CURRENT PRICE

$start_position=strpos($myLine, "class=\"listprice\">") + 18;

# WHY MESS AROUND? END_POSITION IS MAX 15 CHARS FROM START, RIGHT?
# HECK, MAKE IT 40 FOR THE REALLY EXPENSIVE BOOKS!
# SEPARATE THE RESULT INTO AN ARRAY, SPLIT AT THE FIRST "<" CHAR

$myContentA=explode("<",substr($myLine, $start_position, 15));

# THE FIRST ELEMENT IN THE ARRAY IS THE FIRST LIST PRICE
# THE REST OF THE ARRAY IS EVERYTHING AFTER THE FIRST "<" CHAR
# WE DON"T NEED THAT, SO JUST GRAB THE FIRST ELEMENT

$myContent=$myContentA[0];

# BINGO.

echo $myContent;

?>

9:51 pm on Dec 23, 2004 (gmt 0)

Full Member

10+ Year Member

joined:June 16, 2004
posts:232
votes: 0


Ummm..thank you stupidscript but i just have another question. I trieding modding your script to also include the used book price however i keep getting the wrong value for the used books.

the source for the used book is like this

<span class="tiny">from</span>
<span class="price">$49.50</span>

however currenlty the script goes further down the page and pulls out a value of 141.70
the source of that is below

<span class="price">$141.07</span>

<?php
$url_to_grab = "http://www.ama***.com/exec/obidos/ASIN/0534492584/qid=1103837824/sr=2-1/ref=pd_ka_b_2_1/002-9953569-5954449";

if(!($myFile=@fopen($url_to_grab,"r"))) {
echo "Unable to get information from $url_to_grab.";
exit;
}

while(!feof($myFile)) {
$myLine.=fgets($myFile,255);
}
fclose($myFile);

$myLine2=$myLine;

# THIS CODE DIRECTLY PRECEDES THE FIRST LIST PRICE DISPLAY
# I ADDED 18 TO $start_position TO KEEP THE VARIABLE FROM DISPLAYING, TOO
# CHANGE TO class=\"price\" AND + 14 TO GRAB THE FIRST CURRENT PRICE

$start_position=strpos($myLine, "class=\"price\">") + 14;
$start_usedposition=strpos($myLine2, "span class=\"price\">");

# WHY MESS AROUND? END_POSITION IS MAX 15 CHARS FROM START, RIGHT?
# HECK, MAKE IT 40 FOR THE REALLY EXPENSIVE BOOKS!
# SEPARATE THE RESULT INTO AN ARRAY, SPLIT AT THE FIRST "<" CHAR

$myContentA=explode("<",substr($myLine, $start_position, 40));
$myContentB=explode("<",substr($myLine2, $start_usedposition, 40));

# THE FIRST ELEMENT IN THE ARRAY IS THE FIRST LIST PRICE
# THE REST OF THE ARRAY IS EVERYTHING AFTER THE FIRST "<" CHAR
# WE DON"T NEED THAT, SO JUST GRAB THE FIRST ELEMENT

$myContent=$myContentA[0];
$myContentused=$myContentB[0];

# BINGO.

echo $myContent;
echo $myContentused;


?>

8:23 am on Dec 26, 2004 (gmt 0)

Full Member

10+ Year Member

joined:June 16, 2004
posts:232
votes: 0


Ok nearly done my script only a small problem. i mean the script works for the books on amazon which have a price, however it doesnt work for books which doesnt have a price. The thing is i am trying to create a if statement to see if the statement is flase, if it is, then it should use another variable for its starting point.
<?php
$url_to_grab = "http://www.amazon.com/exec/obidos/ASIN/0030317169/qid=1103837824/sr=2-1/";

if(!($myFile=@fopen($url_to_grab,"r"))) {
echo "Unable to get information from $url_to_grab.";
exit;
}

while(!feof($myFile)) {
$myLine.=fgets($myFile,255);
}
fclose($myFile);

$pricecheck=strpos($myLine, "class=\"price\">")+ 14;
if($pricecheck===false){
$pricecheck=strpos($myLine, ">Availability:<");
}

$red=substr($myLine, $pricecheck, 1200);
$myContentA=explode("<",$red);
$myContent=$myContentA[0];
echo "Amazon (New): US".$myContent;

$red2=substr($red, strpos($red, "new<")+62);
$newvar=explode("<",$red2);
$usedprice=$newvar[0];
echo "Amazon (Used): US ".$usedprice;
echo"<br/>";
?>

6:11 pm on Dec 26, 2004 (gmt 0)

Junior Member

10+ Year Member

joined:July 21, 2003
posts:130
votes: 0


Is there a reason why you are doing it this way and not simply using Amazon's webservices facility? They're free and very easy. And no grabbing pages, and no exposure should their page format ever change, or a page not contain the information you need and so on. And there are a variety of (free) scripts out there already to do this, PHP included. Plus many tutorials. Just search google or visit amazon itself, how to do this legitimately is all listed on pages off their homepage.