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?

3 Answers 3

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'));

?>
Sign up to request clarification or add additional context in comments.

8 Comments

Actually I'm trying to avoid using cURL because it's slow. That's the point of my question.
@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.
Yes I'm using cURL, but that is precisely my problem.
@Hassan Then use ajax! Thats the best solution out there!! I too always use ajax, cURL should be always avoided for scripts.
@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!!
|
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.

Comments

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.

1 Comment

Get arguments are sent to specific PHP file. Trying to access them from another file is not what I'm trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.