Curl alternative to require file instead of allow_url_fopen?

I have a php script that I am trying to pull some data from a database on one site and display it on another site. I need to reference some files, but using require filename.php throws errors because my allow_url_fopen is disabled for security reasons. Is there an alternative way to “require” a file using cURL?

OLD example:

<?php require ('http://site.com/filename.php'); ?>

I tried this, but it does not work:

<?php
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, 'http://site.com/filename.php');
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec($ch);
curl_close($ch);

// display file
require $file_contents;
?>
+3
source share
6 answers

Ms. Ramsey

2011 RESTful, Yahoo . REALLLLLLY , .

-, ( ) RESTful, ( ):

// RESTful service
$action = 'action_' . filter_input(INPUT_GET, 'action', FILTER_SANITIZE_STRING);
$params = filter_input(INPUT_GET, 'params', FILTER_SANITIZE_STRING);

if (function_exists($action))
{
    // Run the action w/ the parameters
    $data = call_user_func_array($action, json_decode($params));

    // Encode the data in a HTTP friendly way...
    $output = json_encode($data);

    echo $output;
}
else
{
    trigger_error('Invalid action: ' . htmlspecialchars($action), E_USER_ERROR);
}


function action_foo($param1)
{
    return "Param 1: $param1";
}

, , , , :

$ch = curl_init();
$params = json_encode(array('param1' => 'value', 'param2' => 'value'));
curl_setopt ($ch, CURLOPT_URL, 'http://site.com/restServer.php?action=foo&params=' . $params);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

$data = json_decode($output);

.

, !

+2

, , , /json /xml ( , ).

, , - . . , .

0

, . php curl, php, . , , - - php curl (, php .txt), HUUUUGE, , , !

0

, , echo $contents; . , .

, require/include, , .

0

, , .

, ", ", .

IP , DNS. .

, , (SSL) - , . , . , .

, :

  • eval: , .
  • : . , , , . . .

https://123.145.168.190/file.txt , http://mydomain/file.txt, . curl - .

0

If both sites were under your control, this would be a less practical security risk and a bigger speed issue. If you say this is content, I would only recommend an echo or solution <iframe>.

But anyway, try enabling url fopen wrappers via .htaccess php_flag allow_url_fopen 1for simplicity.
Otherwise, your curling method looks fine. However, it requiredoes not work with the contents of a string, but only with files. Essentially, you are trying:

eval($contents);

To avoid stigma, you can use:

require(file_put_contents(tempnam("/tmp", "rem_"), $contents));
0
source

All Articles