使用php从网站获取数据

时间:2014-12-29 10:08:49

标签: javascript php html html5

我必须建立一个像“Trovaprezzi”这样的网站,并且我必须从不同的网站获取一些数据并在我的网站上显示。 我必须采取的数据是一些链接。 我以为使用PHP,我试图使用CURL。 我有一些问题,因为这不是更好的方式(我下载了所有页面,但我不能只采取链接)。 有人可以帮帮我吗?

<?php 

function cURLcheckBasicFunctions() 
{ 
  if( !function_exists("curl_init") && 
      !function_exists("curl_setopt") && 
      !function_exists("curl_exec") && 
      !function_exists("curl_close") ) return false; 
  else return true; 
} 

/* 
* Returns string status information. 
* Can be changed to int or bool return types. 
*/ 
function cURLdownload($url, $file) 
{ 
  if( !cURLcheckBasicFunctions() ) return "UNAVAILABLE: cURL Basic Functions"; 
  $ch = curl_init(); 
  if($ch) 
  { 
    $fp = fopen($file, "w"); 
    if($fp) 
    { 
      if( !curl_setopt($ch, CURLOPT_URL, $url) ) 
      { 
        fclose($fp); // to match fopen() 
        curl_close($ch); // to match curl_init() 
        return "FAIL: curl_setopt(CURLOPT_URL)"; 
      } 
      if( !curl_setopt($ch, CURLOPT_FILE, $fp) ) return "FAIL: curl_setopt(CURLOPT_FILE)"; 
      if( !curl_setopt($ch, CURLOPT_HEADER, 0) ) return "FAIL: curl_setopt(CURLOPT_HEADER)"; 
      if( !curl_exec($ch) ) return "FAIL: curl_exec()"; 
      curl_close($ch);
      fclose($fp); 
      return "SUCCESS: $file [$url]"; 
    } 
    else return "FAIL: fopen()"; 
  } 
  else return "FAIL: curl_init()"; 
} 

// Download from 'example.com' to 'example.txt' deal-title should-truncate
echo cURLdownload("http://www.groupon.it/browse/salerno?category=mangiare-e-bere&page=2", "example.txt"); 

?>

1 个答案:

答案 0 :(得分:0)

检查Parse Web Pages with PHP script 。可能对你有用。