PHP nasıl bir web sayfasının HTML kodunu alabilirim?

6 Cevap php

PHP bir bağlantı HTML kodunu (web sayfası) almak istiyor. Örneğin, bağlantı ise,

http://stackoverflow.com/questions/ask

sonra servis edilir sayfanın HTML kodunu istiyorum. Ben bu HTML kodunu almak ve bir PHP değişkeni saklamak istiyorum.

Bunu nasıl yapabilirim?

6 Cevap

PHP sunucu url fopen korumalar izin veriyorsa basit yolu:

$html = file_get_contents('http://stackoverflow.com/questions/ask');

Eğer daha fazla denetime gereksinim varsa o zaman cURL fonksiyonları bakmak gerekir:

$c = curl_init('http://stackoverflow.com/questions/ask');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
//curl_setopt(... other options you want...)

$html = curl_exec($c);

if (curl_error($c))
    die(curl_error($c));

// Get the status code
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);

curl_close($c);

http://developer.yahoo.com/yql: Sen Yahoo YQL kütüphaneleri kontrol etmek isteyebilirsiniz

Eldeki görev kadar basittir

select * from html where url = 'http://stackoverflow.com/questions/ask'

Sen de konsolunda bunu deneyebilirsiniz: http://developer.yahoo.com/yql/console (giriş gerektirir)

http://developer.yahoo.net/blogs/theater/archives/2009/04/screencast_collating_distributed_information.html: Ayrıca bazı güzel fikirler daha ne yapabilirim Chris Heilmanns screencast görmek

Eğer ancak kıvırmak daha iyi practive bir değişken olarak kaynağını saklamak isteyen eğer file_get_contents kullanabilirsiniz.

$url = file_get_contents('http://example.com'); echo $url;

Bu çözüm sitenizde web sayfasını gösterecektir. Ancak kıvırmak daha iyi bir seçenektir.

İşte iki farklı, simple ways to get content from URL:

1) ilk yöntem

Barındırma dan allow_url_include etkinleştirin (php.ini veya bir yere)

<?php
$variableee = readfile("http://example.com/");
echo $variableee;
?> 

veya

2) ikinci yöntem

Php_curl, php_imap ve php_openssl etkinleştirin

<?php
// you can add anoother curl options too
// see here - http://php.net/manual/en/function.curl-setopt.php
function get_dataa($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
  curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

$variableee = get_dataa('http://example.com');
echo $variableee;
?>