Jump to content

How to Grab info from another page ...


bsamson

Recommended Posts

Hello,
  I am currently working on an internal site for a wireless dealer. We have an upgrade program in place. The carrier offers a site to check a customers upgrade eligibility. On the page it gives the customers name, phone number, 1 yr upgrade date, and 2 yr upgrade date. Along w/ an "Office Use #" at the bottom.
  Now, in order to pull up this page we have to enter the customers phone # and zip code.  I was wondering if their is a way to "grab" those values from that page w/ php? Any help would be greatly appreciated! Thanks!
Link to comment
https://forums.phpfreaks.com/topic/26711-how-to-grab-info-from-another-page/
Share on other sites

Of course there is. Grab the file using [url=http://php.net/file_get_contents]file_get_contents[/url], then you'll need to parse the file for the infomation. If your real lucky the file might be valid xhtml and you should be able to use the [url=http://php.net/simplexml]simpleXML[/url] extension to get what you want, otherwise, you'll need to do quite a bit of work with regular expressions.
thank you. I did some reading on it. Now, one more question.

there are 2 pages on the carriers site. There is index.php which is where the form resides asking for the customers phone # and zip code. Once that data is entered on that page you are sent to the info.php page, which if everything was entered correctly displays that things i listed above. How in the world in php do I submit the info to the info.php, and then use file_get_contents to get the data. Any suggestions would be GREATLY appreciated!

Best Regards,
Brian
UPDATE.

  I finially figured out how to grab the page w/ cURL. Now I have the page stored in [i]$result[/i]. You said above that I would have to use regular expressions to get the info I need. I just need a bit of advice ...

I need to pull some info off the page ...
I need the customer cell phone #, first name, last name, 1st upgrade date, and 2nd upgrade date. Now after looking at the source this is what I noticed:

#1: The Phone #, First Name, and Last time all begin with the <td class="data"> tag and ofcourse end with the </td> tag.

#2: The 1st and 2nd upgrade dates begin w/ <td> and both end with </td>.

Can I please get some help on the regular expressions I will need to pull this info and put them in a variable? Thanks in advance for all your help!

Sure! And thank you for the reply.

[code]<div class="stacked">
<div class="basicBox">
<h2>Customer Information</h2>
<br />
<table width="100%" border="1" cellpadding="0" cellspacing="0" class="confirmForm tighter">
<tr>
<td class="label">Phone Number:</td>
// This line is the phone #  <td class="data">123-456-7890</td>
</tr>
          <tr>
<td class="label">First Name:</td>
// This line is the first name  <td class="data">GEORGE</td>
</tr>
<tr>
<td class="label">Last Name:</td>
  // This line is the last name  <td class="data">JOHNSON</td>
</tr>
</table>
<div class="hrTight"></div>
<h2>Phone Upgrade Program Results</h2>
<div class="hrTight"></div>
  <div id="rebtable">
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
  <th class="headborder">Savings Amount</th>
  <th> Eligibility Date</th>
</tr>
<tr>
  <td class="displaycell">
    Up to $75 with a two-year subscriber agreement<br>
    (or up to $25 with a one-year agreement)
  </td>
    // This line is the 1st yr upgrade date          <td>06/01/2006</td>
</tr>
<tr><td colspan="2"><hr width="100%"/></td></tr>
<tr>
   <td class="displaycell">
     Up to $150 with a two-year subscriber agreement<br>
    (or up to $75 with a one-year agreement)
   </td>
    // This line is the 2nd yr upgrade date           <td>04/01/2007</td>
</tr>
</table>
  </div>[/code]

Thank you for any help!
[code]<?php

//$html holds the value recived using cURL

$regex = "/<td class=\"label\">Phone Number:<\/td>.*?<td class=\"data\">(.*?)<\/td>.*?<td class=\"label\">First Name:<\/td>.*?<td class=\"data\">(.*?)<\/td>.*?<td class=\"label\">Last Name:<\/td>.*?<td class=\"data\">(.*?)<\/td>.*?<td>([0-9]{2}\/[0-9]{2}\/[0-9]{4})<\/td>.*?<td>([0-9]{2}\/[0-9]{2}\/[0-9]{4})<\/td>/is";

preg_match_all($regex, $html, $matches);

?>[/code]

$matches[1][0] will contain the phone number.
$matches[2][0] will contain the first name.
$matches[3][0] will contain the last name.
$matches[4][0] will contain the first date.
$matches[5][0] will contain the second date.

if there are there's more than one result per page, the next result will be in $matches[1][1] (phone), $matches[2][1] (name) etc'

Orio.
Thank you! Sorry I havent written back sooner, but I have been testing ...
  Let me show the source of the php script page:

[code]<?

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,"http://thewebsite.com/info.php?from=index.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "phoneNumber=3956458546&zipCode=78956");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$result = curl_exec ($ch);
curl_close ($ch);

$regex = "/<td class=\"label\">Phone Number:<\/td>.*?<td class=\"data\">(.*?)<\/td>.*?<td class=\"label\">First Name:<\/td>.*?<td class=\"data\">(.*?)<\/td>.*?<td class=\"label\">Last Name:<\/td>.*?<td class=\"data\">(.*?)<\/td>.*?<td>([0-9]{2}\/[0-9]{2}\/[0-9]{4})<\/td>.*?<td>([0-9]{2}\/[0-9]{2}\/[0-9]{4})<\/td>/is";

preg_match_all($regex, $result, $matches);

$no = $matches[1][0];
$first = $matches[2][0];
$last = $matches[3][0];
$oneyr = $matches[4][0];
$twoyr = $matches[5][0];

echo $no;
echo $first;
echo $last;
echo $oneyr;
echo $twoyr;

?>[/code]

Now, when I run this script it just displays a blank page. Any Ideas?
I tested it, and it works fine on my end.
See links here to the [url=http://www.oriosriddle.com/html.txt]HTML[/url] you gave me, and [url=http://www.oriosriddle.com/test.php]the result[/url] of running this:
[code]<?php

$html = file_get_contents("html.txt");

$regex = "/<td class=\"label\">Phone Number:<\/td>.*?<td class=\"data\">(.*?)<\/td>.*?<td class=\"label\">First Name:<\/td>.*?<td class=\"data\">(.*?)<\/td>.*?<td class=\"label\">Last Name:<\/td>.*?<td class=\"data\">(.*?)<\/td>.*?<td>([0-9]{2}\/[0-9]{2}\/[0-9]{4})<\/td>.*?<td>([0-9]{2}\/[0-9]{2}\/[0-9]{4})<\/td>/is";

preg_match_all($regex, $html, $matches);

$no = $matches[1][0];
$first = $matches[2][0];
$last = $matches[3][0];
$oneyr = $matches[4][0];
$twoyr = $matches[5][0];

echo $no; echo "<br>";
echo $first; echo "<br>";
echo $last; echo "<br>";
echo $oneyr; echo "<br>";
echo $twoyr; echo "<br>";

?>[/code]

Orio.

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.