Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

php curl


ghost's Avatar
0 0

Hello, here is what I am trying to do: use curl to get info from page2.php that is running the script:

$i=0;
while($i < 100){
	$i++;
	sleep(1);
	echo $i." ";
    flush();
    ob_flush();
}
?>```

I need the page1.php to update as page2.php updates.

Is there any way to do this?

ghost's Avatar
0 0

So then, would it be sufficient to just have page1.php use curl to grab the page information from page2.php and echo it as it's own page? If that were the case, why don't you just use include(page2.php); in page1.php? It seems kinda impractical to use cURL to use it to update page1.php with page2.php's inormation constantly because it will only work while the script is being executed on the server. What exactly do you need, do you need certain information to be echoed on page1.php from page2.php or just replicate it?


ghost's Avatar
0 0

I never thought of using include, i will play around with that, but in the end, page1 and page2 will be on different servers.


ghost's Avatar
0 0

Ah, if it's remote then your best bet would probably be to use readfile(). But, it shouldn't be too terribly difficult to use curl either I guess, it would seem simple enough as doing something like:

<?php
$i = 1;
while($i > 0){
$url=("page2.php");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$result = curl_exec($ch);
curl_close($ch);
echo($result);
$i++;
}
?>

mirite?? :p

[edit] Oops forgot a line [/edit]


ghost's Avatar
0 0

That script will wait until the script on page2 finishes, then it loads it onto page1, which isn't quite what I want done… I'm thinking it cant be done with curl.

edit Looks like the only way to do it is with javascript/ajax… that shall be a pain in the rear.


ghost's Avatar
0 0

Feralas wrote: That script will wait until the script on page2 finishes, then it loads it onto page1, which isn't quite what I want done… I'm thinking it cant be done with curl.

edit Looks like the only way to do it is with javascript/ajax… that shall be a pain in the rear.

Erm..no it won't. It is an infinite loop where it constantly gets the HTML data from page2.php, stores it as $result and echoes the HTML and/or javascript onto the page. It does not wait for a page to stop loading to update etc..It just basically keeps the page updated with a loop of curl queries and doesn't stop while the PHP page is loaded. It won't just automatically update it's self invisibly, the PHP must be executed though. I'm not at all saying that was the easiest/most effective or efficient way to do it in fact, it's not. But meh, it can be done with PHP probably easier than javascript or at least more efficiently.


ghost's Avatar
0 0

I cant get it to work for some odd reason. Ive tried multiple options with it.