Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

asynchronious call to another php page from php

Posted on 2007-11-25
5
Medium Priority
?
746 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 21

Assisted Solution

by:nizsmo
nizsmo earned 600 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 1400 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

604 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