Php kullanarak bir url içeriği alın

3 Cevap

Belirli bir url gelen dinamik içeriğini almak istiyorum:

Ben kodu kullanmış

echo $content=file_get_contents('http://www.punoftheday.com/cgi-bin/arandompun.pl');

Ben şu sonuçlar alıyorum:

document.write('"Bakers have a great knead to make bread."

') document.write('© 1996-2007 Pun of the Day.com
')

How can i get the string Bakers have a great knead to make bread. Only string inside first document.write will change, other code will remain constant

Selamlar,

Pankaj

3 Cevap

Bir komut dosyası tarafından sorgulanan değil, doğrudan belgeye inşa edilecek gerekiyordu bir JavaScript pasajı getiriliyor edilir. Içinde kod JavaScript.

Bir düzenli ifade kullanarak kod çekin, ama ben buna karşı tavsiye ederiz. İlk olarak, muhtemelen yapmak yasal değil. İkincisi, hizmet ettikleri verilerin biçimi komut kırma, istediğiniz zaman değiştirebilirsiniz.

Sana their RSS feed adresinde almak gerektiğini düşünüyorum. Sen JavaScript daha programlı şekilde daha kolay olduğunu ayrıştırmak.

Bunu nasıl bu soruyu göz atın: Best way to parse RSS/Atom feeds with PHP

Major PHP methods TO GET CONTENT from URL

(Note: if you want to get the images and links correctly from the target link, you may need to re-make content before output. for example, change href="./imageblabla.png" to href="http://site.com/imageblabla.png")

1) php.ini de, sizin hosting (İlk başta Allow_url_ {[(0 etkinleştirmek)]} ya da bir yerde)

<?php 
//you may use "r" instead of "rb"
$variablee = fopen('http://example.com/', "rb");  
echo stream_get_contents($variablee);  
?>

OR

2)

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

OR

3) (ilk başta, etkinleştirmek php_curl, php_imap, php_openssl)

<?php
// you can add anoother curl options too
// see here - http://php.net/manual/en/function.curl-setopt.php
function get_data($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  //curl_setopt($ch, CURLOPT_POST, TRUE);             // Use POST method
  //curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=1&var2=2&var3=3");  // Define POST data values
  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;
}

$variablee = get_data('http://example.com');
echo $variablee;
?>

OR

4)

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

OR

5) (İlk başta Allow_url_ etkinleştirmek include)

<?php 
$variablee = include "http://example.com/"; 
echo $variablee;
?>

Pekka cevabı muhtemelen bunu yapmanın en iyi yoludur. Ama yine de burada kendinizi böyle bir şey yaptığını bulmak davayı kullanmak isteyebilirsiniz regex, ve RSS beslemeleri vb güvenemez

document\.write\('      // start tag
([^)]*)                 // the data to match
'\)                     // end tag

EDIT, örneğin:

<?php
$subject = "document.write('&quot;Paying for college is often a matter of in-tuition.&quot;<br />')\ndocument.write('<i>&copy; 1996-2007 <a target=\"_blank\" href=\"http://www.punoftheday.com\">Pun of the Day.com</a></i><br />')";
$pattern = "/document\.write\('([^)]*)'\)/";
preg_match($pattern, $subject, $matches);
print_r($matches);
?>