Solved

asynchronious call to another php page from php

Posted on 2007-11-25
5
735 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now