Jump to content

Web page scrape php problem


Lodgey

Recommended Posts

Hey all, im fairly new to php coding and wondered if anyone can help me either create or simplify a guide for me.

I am trying to create a script that will get stats off a webpage and post then put them into a picture on a webpage, i have a guide but dont really understand much of it, i had created one before but it has very limit use.

I will post the script that i start to much which was mostly cutting and pasting from the guide then i will post the guide i got.

Simplifying the guide to simplest terms would really help as i would like to make the script for other aswell.

Here is the script i done so far:

<?
//Put the URL into a variable so its easier to code with

$url = "http://events.wwiionline.com/scripts/services/persona/service.jsp?pid=418851";

//Extend the time out, sometimes CSR takes a while

set_time_limit(30);

//Append the players pid onto the URL and read the page into the $page variable
//as an array

$page = file($url . $_POST['418851']);
if (!is_array($page))
{
echo "No Array found";
exit;
}

$page = trim(implode("\n", $page));

$pattern = "/<td align=\"right\">(.*)<\/td>\n/i";
if (preg_match_all($pattern, $page, $out, PREG_PATTERN_ORDER))
{
$callsign = $out[1][0];
$enlisted = $out[1][1];
$side = $out[1][2];
$country = $out[1][3];
$branch = $out[1][4];
$rank = $out[1][5];
$sorties = $out[1][6];
$successes = $out[1][7];
$rtbs = $out[1][8];
$rescues = $out[1][9];
$mia = $out[1][10];
$kia = $out[1][11];
$captures = $out[1][12];
$damages = $out[1][13];
$kills = $out[1][14];
$deaths = $out[1][15];
$ratio = $out[1][16];
$favunit = $out[1][17];
$bestunit = $out[1][18];
$worstunit = $out[1][19];
}

if($sorties=="")
$sorties = 0;

if($kills=="")
$kills = 0;

$im = imagecreatefromgif("temp".strtolower($callsign)."any_old_image.GIF");

$white = imagecolorallocate($im, 255, 255, 255);

?>
Link to comment
https://forums.phpfreaks.com/topic/25833-web-page-scrape-php-problem/
Share on other sites

Here is the guide:

//Step 1 SCRAPING

//First you need to have an undertanding of php and perl.  Get the stat page you
want to scrape the data from.  Lets use me as an example.  Here is the link to my
campaign stats for the german army:

http://csr.wwiionline.com/scripts/services/persona/service.jsp?pid=107059

//Put the URL into a variable so its easier to code with

$url = "http://csr.wwiionline.com/scripts/services/persona/service.jsp?pid=107059";

//Extend the time out, sometimes CSR takes a while

set_time_limit(30);

//Next you need to put the page into an array
//Append the players pid onto the URL and read the page into the $page variable
as an array

$page = file($url . $_POST['pid']);
if (!is_array($page))
{
echo "No Array found";
exit;
}

//Put the page in the $page variable and use trim(explode) on it

$page = trim(implode("\n", $page));

//Next is the pattern used to scrape, be careful, if the player doesn't have any
sorties at the bottom of the page, it may cockup!  If you wan't to learn more on
how to scrape web pages with php, just do a google search.  There is plenty of
info on it out there on the web.  The following code is going to search the page
for anything that is aligned to the right in each row of the table.  Then the
next line of code stores each piece of output data into variables.  For example,
line #6 contains sortie information so the variable is $out[1][6].  If you need
to know what line your looking for just put an echo $out[1][X] where X is a
number.  It will spit out a value after you run the script and you can compair it
to the stats page to see what it is.

$pattern = "/<td align=\"right\">(.*)<\/td>\n/i";
if (preg_match_all($pattern, $page, $out, PREG_PATTERN_ORDER))
{
$callsign = $out[1][0];
$enlisted = $out[1][1];
$side = $out[1][2];
$country = $out[1][3];
$branch = $out[1][4];
$rank = $out[1][5];
$sorties = $out[1][6];
$successes = $out[1][7];
$rtbs = $out[1][8];
$rescues = $out[1][9];
$mia = $out[1][10];
$kia = $out[1][11];
$captures = $out[1][12];
$damages = $out[1][13];
$kills = $out[1][14];
$deaths = $out[1][15];
$ratio = $out[1][16];
$favunit = $out[1][17];
$bestunit = $out[1][18];
$worstunit = $out[1][19];
}

if($sorties=="")
$sorties = 0;

if($kills=="")
$kills = 0;

//Step 2 APPLYING THE SCRAPED DATE TO AN IMAGE

//I will supply you with the image font file named proggyclean.gdf.  But here is
a link to a page that contains more font files, use whatever suits your needs.
http://www.widgnet.com/gdf_fonts/fonts.html

//Also, do a google search on "imagestring", you can learn how to apply text to
an image many different ways.

//Font file for image
$font = imageloadfont("proggyclean.gdf");
$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);

//Image template to use for the signature, I store mine in a directory named
"temp" but you can keep it in the same directory as your script if you want to.
Just make sure you have the pathing set

$im = imagecreatefromgif("temp/".strtolower($callsign)."any_old_image.gif");

//Some predefined colors, change them to what you need

$white = imagecolorallocate($im, 255, 255, 255);
$red  = imagecolorallocate($im, 255,0,0);
$black = imagecolorallocate($im, 0,0,0);
$grey = imagecolorallocate($im, 128,128,128);

//strings to put on the image, I just use 4 types of scraped data for my sig, but
you can use anything you want.  The 10 and 120 are the X and Y coordinates on my
image.  You will need to play with the numbers untill you get the right ones for
your image.

imagestring($im, $font, 10, 120, "Sorties: ".$sorties, $white);
imagestring($im, $font, 10, 140, "Kills: ".$kills, $white);
imagestring($im, $font, 10, 160, "K/D: ".$ratio, $white);
imagestring($im, $font, 10, 180, "Caps: ".$captures, $white);

//Store the image in the sigs folder and tidy up the memory footprint.  I do this
because I generate multiple sigs from my script so I save them as callsign.gif so
dingles.gif for example.  Again, I put them in a subfolder named "sigs", you
don't have to do that, keep it in the same directory as your script if you want.

imagegif($im,"sigs/ribbon_dark/".strtolower($callsign).".gif");
imagedestroy($im);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.