bir php sayfasından döndü metni nasıl

5 Cevap

çok basit bir soru php,

Örneğin, demo.php sadece bu "merhaba" gibi bir metni döndürür.

nasıl ben başka bir php sayfası, bir bu metni alabilirim?

GÜNCELLEME:

aslında ben sayfa bu gibi çıktısı anlamına geliyordu "baskı 'merhaba';"

5 Cevap

Does it return "hello", öyle output hello ya da yapar sadece contains hello? Her üç senaryo farklı sorunları vardır.


Eğer return "hello"; gibi:

<?php
return "hello";

sonra kolayca dosya dahil ve dönüş değeri kapma değerini çekebilirsiniz:

<?php
$fileValue = include('secondFile.php');

Eğer outputs hello örneğin,

<?php
echo "hello"; // or print "hello";

Eğer sonuç yakalamak için çıktı tamponlama kullanmanız gerekir:

<?php
ob_start();
include('secondFile.php');
$fileValue = ob_get_contents();
ob_end_clean();

Eğer contains hello örneğin,

hello

sadece sonucu okuyabilirsiniz:

<?php
$fileValue = file_get_contents('secondFile.txt');

See Also:

Eğer "döner merhaba" ile ne demek istiyorsunuz?

Gerçekten olarak bunu döndürür

return "hello";

Eğer sadece bu gibi değerini alabilirsiniz:

$var = include 'demo.php'

o echo yerine bu değeri es eğer, onun çıkış okuyabilirsiniz:

$var = file_get_contents("http://host/demo.php");

EDIT: Ben başlangıçta bir senaryo üzerinde file_get_contents yürütme çıkışı (ve kod) yakala farz etmişti. Eğer isterseniz output, tam URL'yi belirtmeniz gerekir:

$str = file_get_contents("http://example.com/demo.php");

http://php.net/manual/en/function.file-get-contents.php

Eğer daha detaylı cevaplar biri kabul eğer muhtemelen daha iyi olurdu.

Aynı zamanda, aşağıya bakınız:

file_get_contents, ancak kıvrılıp en basit çözümü çok daha etkili olduğunu. Bu, daha hızlı, daha güvenli ve daha esnek bulunuyor.

function file_get_contents_curl($url){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,1);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}

$page = file_get_contents_curl('demo.php');

PHP sayfasını yorumladığı zaman metin döndürülür - ya var demektir:

  • Komut satırından PHP çalıştırın
  • Ya da sunucunuza aracılığıyla sayfayı çağırmak - genelde yapmak eğiliminde olacak ne olduğunu.


In the second case, you need to send an HTTP request, and fetch the result, which can be done using file_get_contents (if the allow_url_fopen configuration directive is enabled) :

$content = file_get_contents('http://www.yoursite.com/demo.php');


Another solution, especially useful when allow_url_fopen is disabled, is to use curl ; see for instance the examples on the page of the curl_exec function.