Solved

asynchronious call to another php page from php

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

860 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