1

I've been wanting to do this because my site does about 3 HTTP requests per page load, because each PHP's output is retrieved with cURL. This is too slow, and I want to avoid using cURL. I found this question on Stack Overflow, and it basically does what I want. The accepted answer's suggestion is to use ob_start(); to start getting output then use ob_get_clean(); to put the output into a variable. My issue now is that the PHP scripts I'm trying to capture output from need variables passed to them using HTTP Get. The access those variables like this:

$term = $_GET['term'];

And I don't want to change that, because although I'm going to access these PHP scripts' outputs from another PHP script, I'm also planning on accessing them from elsewhere. So, is there a way to fool these PHP scripts into accepting some arguments through Get, then capturing their outputs with the method suggested above?

Community
  • 1
  • 1

3 Answers3

2

You can $_GET variables from any php script if its set (use isset to check that). Then just cURL to such url's will work.

If you have changed the method to POST earlier, you can use CURLOPT_HTTPGET. See the curl_setopt functions page (http://www.php.net/manual/en/function.curl-setopt.php) for more details.

For a non-cURL method, use jQuery ajax. It is quite simple to use, just read the documentation here.

EDIT: This is what you wanted (haven't checked the code though)

<?php

function get_include_contents($filename, $get) {
 if (is_file($filename)) {
    ob_start();
    $_GET = array();
    while (list($key, $val) = each($get)) {
      $_GET[$key]=$val;
    }
    include $filename;
    return ob_get_clean();
 }
 return false;
}

$string = get_include_contents('somefile.php', array('param1'=>'x', 'param2'=>'y'));

?>
gopi1410
  • 6,567
  • 9
  • 41
  • 75
  • Actually I'm trying to avoid using cURL because it's slow. That's the point of my question. –  Jun 12 '12 at 08:31
  • @Hassan You actually said that you are using cURL in first line of your question. Anyways then u can use `jQuery ajax` for this. Just pass the values which you want to send as `get` values in `data` parameter of $.ajax & your output will be received in the success function. – gopi1410 Jun 12 '12 at 08:34
  • Yes I'm using cURL, but that is precisely my problem. –  Jun 12 '12 at 08:34
  • 1
    @Hassan Then use ajax! Thats the best solution out there!! I too always use ajax, cURL should be always avoided for scripts. – gopi1410 Jun 12 '12 at 08:37
  • AJAX would be just as slow, since it would still have to make several requests to the server. I'm trying to use the method [here](http://stackoverflow.com/questions/171318/how-do-i-capture-php-output-into-a-variable), as described in the question. –  Jun 12 '12 at 08:39
  • @Hassan ok, just a sec, I got u. will be updating my answer soon – gopi1410 Jun 12 '12 at 08:47
  • 1
    @Hassan gopi1410 Awesome answer! I like the way of answering it as a function. & iteration using list & each function is cool! never thought of using it in such a way. This should have been the accepted answer!! –  Jun 12 '12 at 09:03
  • @johntheripper Yes it's a very good answer, and I upvoted it. But I had accepted Berry's answer already when gopi understood what I was asking. –  Jun 12 '12 at 09:11
1

And I don't want to change that, because although I'm going to access these PHP scripts' outputs from another PHP script, I'm also planning on accessing them from elsewhere. So, is there a way to fool these PHP scripts into accepting some arguments through Get, then capturing their outputs with the method suggested above?

Your question is a bit unclear as to why you're using cURL in the first place. If your scripts are on the same server, you can simply set the correct $_GET variables and use:

<?php
ob_start( );
// include the file.
$_GET['foo'] = 'bar';
include 'the_file.php';

$output = ob_get_clean( );

If your scripts are located on another server, where include is not viable, you will always have to do a HTTP request to get their contents, regardless of whether your this with cURL or Ajax, or sockets for all I care.

Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
0

well you can access a $_GET from any script loaded as long as its in the URI, the variable $term can be used in any script. You can also include the script.

When you include a script you can access some of its content after the include.

Eli
  • 4,329
  • 6
  • 53
  • 78
  • Get arguments are sent to specific PHP file. Trying to access them from another file is *not* what I'm trying to do. –  Jun 12 '12 at 08:33