Solved

asynchronious call to another php page from php

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.php tree directory? 5 54
WordPress Themes 10 34
Convert html page to a php post email form automatically? 10 16
Configuring a checkbox in CSS and php 18 27
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
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 look for a specific file type in a local or remote server directory using PHP.

919 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

12 Experts available now in Live!

Get 1:1 Help Now