Is it possible to capture PHP code from a page using CURL?

Asked

Viewed 3,429 times

0

How do I execute the PHP code of a particular page within my application, and I do not own this application.

  • What do you mean? What do you want to do?

  • I want a line of php code that runs a php file from another server.

  • You just need to include the file within the current file. include, include_once, require or require_once.

  • Good evening, it’s not quite what I want, what I want is to use Curl to read another php file

  • So explain better, the question is not very clear. If you don’t have code, exemplify in more detail what you want to do.

  • For example. I have the x file on the server a and I have the y file on the server b. I want the x file to have a line that runs the y file on the b server. Everything with php

  • 1

    Running a code from another page is quite different from "read another page". be specific ;)

Show 2 more comments

2 answers

2


It is not possible to execute a third-party PHP code because the PHP code is sent to the server, processed and then returns an HTML output, what you can do is capture only this output, and use only the data input parameters, such as GET or POST.

CURL just captures the page as it was processed by the server (if the third party server allows it and you have CURL installed in your apache), if you want to use only that, this is the code to capture:

function capturarUrl($url_metodo)
{

    try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url_metodo);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $saida = curl_exec($ch);
            curl_close($ch);
    } catch (Exception $e) {
            $saida = file_get_contents($url_metodo);
    }
    return $saida;
}

echo capturarUrl('http://www.google.com');

1

Here, but I don’t recommend it, because I only use Curl to take a page’s HTML and handle it. For example, when I want the inflation data from the Central Bank website, I use Curl.

#cria uma variável com a inicialização da função
$curl = curl_init();

#passa as opções para a função cURL
curl_setopt($curl, CURLOPT_URL, "caminho/pro/seu/arquivo.php"); //URL do arquivo .PHP. A URL pode ser passada por variavel também $url = "caminho/pro/seu/arquivo.php"
curl_setopt($curl, CURLOPT_HEADER, FALSE); //nao incluir os cabeçalhos na saida/resultado
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); //retorna o curl_exec() como uma string, em vez de imprimir diretamente

#executa a sessão cURL
$resultado = curl_exec($curl);
#fecha a sessão cURL
curl_close($curl);

#imprime o resultado
echo $resultado;

To curl_init() can receive the URL directly as well: curl_init("caminho/do/arquivo.php"). In that case, you can remove curl_setopt($curl, CURLOPT_URL, $url.

  • -1 It will get HTML from the page, not PHP which is the reason for the question. Your answer leads to believe that it would be possible to get PHP using this, although only use for HTML.

  • Bro, c'mon! can’t get PHP from the page, have common sense, for goodness sake. PHP runs on the SERVER, to have access to it you have to have access to the server, which will only happen if you are administrator or have access to it in any way, including illegally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.