FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Using PHP toreciever information from external source.





cr3ativ3
Ok so what I am working on right now is this: I am trying to use php to go to a webpage (external) find a section on the page such as a word and then find the first occurences of a word after that part and then send it back to the page the script is on and display certain information. See what I am doing is trying to display external data about bus delays on one page when he delays are on some external source.

So I have a php script and it is going to go to: http://www.schoolbusmonitor.com/london/viewschools.php?cID=1&bID=3&rID=5 and find the saunders section and find if there are delays or no delays and then display on the php page a message saying it is delayed or it is not delayed.

Hopefully you understood that?
MrBlueSky
2 questions:

1. Do you only want the information on the 'Saunders'-item, or on more items?

2. Can you give the program you have so far?

Maybe I can help you
cr3ativ3
Uhh well Idon't really have a program right now I was just wondering the basis of how to grab that information and then echo it out to the user. I would be greatly apreciative if you could help.
MrBlueSky
Here you go:

Code:

<?php

$service_port = getservbyname('www', 'tcp');

$address = gethostbyname('www.schoolbusmonitor.com');

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

$result = socket_connect($socket, $address, $service_port);

$in = "GET /london/viewschools.php?cID=1&bID=3&rID=5 HTTP/1.1r\n";
$in .= "Host: www.schoolbusmonitor.com\r\n";
$in .= "Connection: Close\r\n\r\n";

$out = '';

socket_write($socket, $in, strlen($in));
$res = "";
while ($out = socket_read($socket, 2048)) {
    $res = $res.$out;
}

socket_close($socket);

preg_match('/Saunders[\s\S]*?com\/([^-]+)/i', $res, $result);
if ($result[1]=="green") {
  // No delay
  // PUT YOUR CODE HERE
  echo "No delay";
}
if ($result[1]=="red") {
  // Delay
  // PUT YOUR CODE HERE
  echo "Delay";
}
if ($result[1]=="question") {
  // No report
  // PUT YOUR CODE HERE
  echo "No report available";
}

?>


Enjoy
manum
dude u need to read about cURL and PHP

cURL will help u get all the info u need

it's supported by frihost also......
MrBlueSky
Explanation

First note
1) You can read about sockets in PHP here
If you're not familair with the socket-concept and not going to use sockets in your PHP scripts
in the near future I don't recommend reading it. Just take my word for it that you can use
them to retrieve data from other servers.
2) You can read about Regular Expressions in PHP here.
If you are not already familiar with Regular Expressions I recommend you learn how to use them: they
are very powerfull and can help you solve a lot of text-related problems. Start here or here

Code:

<?php

$service_port = getservbyname('www', 'tcp');

$address = gethostbyname('www.schoolbusmonitor.com');

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

$result = socket_connect($socket, $address, $service_port);


We create a socket to communicate with schoolbusmonitor.com on the standard WWW port (80)

Code:

$in = "GET /london/viewschools.php?cID=1&bID=3&rID=5 HTTP/1.1r\n";
$in .= "Host: www.schoolbusmonitor.com\r\n";
$in .= "Connection: Close\r\n\r\n";

$out = '';


Here I create the message which we're going to send to the server. It is a simple get-request which
requests a webpage from the server using a query-string.

Code:

socket_write($socket, $in, strlen($in));


Send the request to the server at schoolbusmonitor.com.

Code:

$res = "";
while ($out = socket_read($socket, 2048)) {
    $res = $res.$out;
}

socket_close($socket);


We read all the data which schoolbusmonitor.com sends us back (which is plain HTML)

Code:

preg_match('/Saunders[\s\S]*?com\/([^-]+)/i', $res, $result);


I use a Regular Expression to extract the part of the page which is of interest for us. For someone who
is new to Regular Expressions it looks a bit intimidating, but it does the trick.

Code:

if ($result[1]=="green") {
  // No delay
  // PUT YOUR CODE HERE
  echo "No delay";
}
if ($result[1]=="red") {
  // Delay
  // PUT YOUR CODE HERE
  echo "Delay";
}
if ($result[1]=="question") {
  // No report
  // PUT YOUR CODE HERE
  echo "No report available";
}


And based on the result we're going to take some action...
cr3ativ3
Interesting.... I like what you can do with sockets and expressions like you did so that's why I asked you to explain it so I could use it on other things.

thanks for taking the time to write that.
MrBlueSky
If you have further questions, just ask them. I am not very good at explaining things though Rolling Eyes
Related topics
[tutor] How to protect images without htaccess using PHP
Using PHP
Includ Images using PHP.
How to start using PHP? Help!
Website Designing using PHP
how can i setup a page break for printer on html using php ?
Reading word documents using PHP?
I am trying to build a contact form using php and flash
Communicating between web pages
how to compress a file using php ?
Checking if ports are up using PHP
Hot to change "Reset Ownership" using php?
Using php to change content of a .txt
How to get the tree structure done using PHP
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.