2

I'm trying to build a monitor of sorts with the use of cURL to keep track of various different dashboards. The idea is that I load a bunch of URLs which I want to see from a database, and after running this code on a browser window, I'm shown those URLs for a limited time (For example, I'm shown www.dashboard.zopim.com for 10 seconds, then it goes onto another different URL for 30 seconds, and so on, and when it ends, it begins again).

<?php

include_once('connection.php');

$query = "SELECT url, time FROM $tb ORDER BY position";
$result = mysqli_query($connection_, $query);

$rowcount = mysqli_num_rows($result);

$curl_handle = curl_init();
$data = array();

//base array building
while ($row = mysqli_fetch_array($result, MYSQLI_NUM)) {
    $data_b = array();
    $data_b['curl_handle'] = $curl_handle;
    $data_b['url'] = $row[0];
    $data_b['time'] = $row[1];
    array_push($data, $data_b);
}

for ($i=0; $i < $rowcount; $i++) {
    $ch = $data[$i]['curl_handle'];
    curl_setopt($ch, CURLOPT_URL, $data[$i]['url']);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1);

    $grab = curl_exec($ch);
    usleep($data[$i]['time']*100);
    curl_close($ch);
    return $grab;
}
?>

The problem is that I cannot even get it to start running. As it is right now, it just loads the first page and remains there.

Also, as an off-comment, since I'm quite unfamiliar with cURL as a whole, is there a better way to do this? I've read that there's a multi function that allows me to load multiple URLs asynchronously, but I'm not sure it'd serve me for what I want.

lower
  • 29
  • 1
  • 5
  • I think most of this is going to have to rely on JS of some sort. – Kaylined Feb 01 '17 at 04:02
  • I'm not allowed to tamper directly with the servers or anything of the sort, so it'd have to limited to the code itself. Care to share a suggestion? – lower Feb 01 '17 at 04:28
  • I think your best option here is to push all the URLS at once to your JS, and have an IFRAME setup to display the site, and have the JS refresh the – Kaylined Feb 01 '17 at 04:37
  • Sadly, I've already tried the iframe approach, and it is no good, as the iframes are blocked, leaving me back at square one. – lower Feb 01 '17 at 04:51

1 Answers1

0

The reason that the script ends after loading the page is that in PHP, when you use return outside of a function, it terminates execution of the script, and you have return at the end of your loop block.

(See http://php.net/manual/en/function.return.php for the documentation of return.)

You can't really do the whole thing you are looking to do with PHP. PHP runs on the server, and the server can't just send an instruction to the browser to do something if all the browser does is send a request, receive a response, and end the connection (which is what usually happens). You need something on the client side to get the browser to take action.

As Kaylined said, Javascript is probably the way to go. I'm sure you could use Javascript to continually fetch more information from your server somehow, and just set up your server to react to these requests from Javascript. On the other hand, if all you want to do with PHP is looking up content from other locations on the internet and passing it directly to the browser like you're doing now, you could probably just as easily have your page include a Javascript script which does accesses the dashboards you want to look at directly.

EDIT: If you really want to just use PHP for this, you could have it send a header which causes the page to refresh (redirect to itself) after a given time. Then you could either use URL parameters with GET or a session variable to determine which page your PHP script should send back, and do it. (See Page redirect after certain time PHP)

Essentially, you would add a line like

header("refresh:30;url=this.php");

before you send any content, where the url is the url of your monitor page, not this.php. Then you would send the page to display. Then, after thirty seconds, the browser would request your page again, and you could send back a different page, with the same header. Of course, you would also want to have some mechanism that lets the server keep track of which page it should send. One thing to be aware of is that this header isn't standard, so it is more likely that some browsers will ignore it, which would be a problem.

Community
  • 1
  • 1
  • Thanks for the swift response. The main problem I have right now is that I'm very limited in the use of javascript, as my boss has made a clear demand on using it as little as possible, hence me recurring to this library I have no real experience with. I cannot tamper with the servers in any measure, hence this real headache. I guess I'll look at implementing a Javascript onto this code as you suggest at the last part of your comment. – lower Feb 01 '17 at 04:26
  • Whoa, didn't see your edit until now. But if I use the header redirection method wouldn't it mean scrapping the whole cURL thing? – lower Feb 01 '17 at 04:49
  • No, not exactly. I can outline what I think should work in the answer. I think it probably would be best to use some sort of Javascript to do this, but it should be possible to do it without any if necessary. – Nathaniel Verhaaren Feb 01 '17 at 05:03
  • Sadly, the header method doesn't seem to work. I made a `for` loop using the variables of my array... and I always get a blank page. – lower Feb 01 '17 at 05:05
  • If you keep the script the same, except you replace the `sleep` line with a header line like above, does your page display correctly and automatically refresh? Because if it does, you could probably make it work. – Nathaniel Verhaaren Feb 01 '17 at 05:13
  • When I replace the `usleep` with `header` I do get the page to display, but the pages don't cycle, and I also get `Warning: Cannot modify header information - headers already sent by (output started at C:\wamp64\www\monitor5\monitor.php:31) in C:\wamp64\www\monitor5\monitor.php on line 32`, which references the `$grab = curl_exec($ch);` line and what would become the `header` line. – lower Feb 01 '17 at 13:28
  • So that error means that some input is being sent before the `header` line is reached, which is an error. [This page](http://php.net/manual/en/function.header.php) suggests that this comes from having a newline or space outside of the PHP tags in the `connection.php` file you include, which is sent at output. So if you remove any newlines or space of any kind outside of PHP tags in that file, that might eliminate this error. It won't cycle through the pages, though, until you make some other modifications to your script. This is just a way to test whether the header works as expected. – Nathaniel Verhaaren Feb 01 '17 at 17:20