Solved

asynchronious call to another php page from php

Posted on 2007-11-25
5
740 Views
Last Modified: 2013-12-13
I have one PHP page which reads from a database, does some mussing about and formatting, and then outputs some html based on what it read.

I have a second php page which I'd really like to "call" this first php page and get a hold of the HTML it would have outputted. Preferably I'd like to pass in HTTP POST fields, although I could work around that.

This is sort-of like the server-side equivalent of ajax. Query another page and get the output of it somehow. Both php files are mine, on the same server, but I don't want to use include(), I want to keep the two operations seperate.

Is this possible to do in php?
0
Comment
Question by:Frosty555
5 Comments
 
LVL 21

Assisted Solution

by:nizsmo
nizsmo earned 150 total points
ID: 20348976
This is an interesting question, you can actually use the below script utilizing the CURL library to do this, though CURL is normally used to retrieve pages outside or in a different domain to your own script, you can also use it locally. Change the localhost test.php url to suite your needs, note that here, relative paths do not work, so you have to specify an absolute path or web address.
<?php
 
// create a new curl resource
$ch = curl_init();
 
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://localhost/testing/test.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// grab URL, and return output
$output = curl_exec($ch);
 
// close curl resource, and free up system resources
curl_close($ch);
 
// Print output
echo $output;
 
?>

Open in new window

0
 
LVL 21

Expert Comment

by:nizsmo
ID: 20348979
More info on CURL:
http://www.php.net/curl

hope this helps.
0
 
LVL 15

Accepted Solution

by:
babuno5 earned 350 total points
ID: 20348997
i agree CURL is an option
But it requires curl to be installed
Another equivalent is using fsockopen
http://in2.php.net/manual/en/function.fsockopen.php
Check out the url it has lots of examples  
And in your case the host will be localhost or 127.0.0.1
0
 
LVL 2

Expert Comment

by:guston76
ID: 20349225
Try this :

// Open the first PHP page as if it is an ordinary file
$f = fopen('http://...', 'r') or die('Cannot open URL');
$s = '';
while (!feof($f))
   $s .= fgets($f);
fclose($f);
 
// $s is the HTML output from the first page

Open in new window

0
 
LVL 31

Author Comment

by:Frosty555
ID: 20528441
Both solutions worked equally well, but I ended up using the fsockopen() method. Both required I put some folder names behind 127.0.0.1 since I'm on a shared webhost, but it works :P.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PHP function parameters defined 14 39
php subtract from date 2 26
Echo'd values in dropdowns 6 27
Why can I INSERT a DATETIME record into mysql but I can't UPDATE it? 15 32
This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question