2014-09-19 12 views
3

我有一些代碼的PHP頁面轉換爲HTML:如何在同一臺服務器上使用php curl或file_get_content獲取url內容?

$dynamic = "http://website.net/home.php"; 
$out = "home.html" ; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,"$dynamic"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$file = curl_exec($ch); 

file_put_contents($out, $file); 

這完美的作品在本地主機,但它需要太多的時間/不直播現場工作。我試過php_get_contents,但這也行不通。

注:

  • http://website.net/home.php頁面是在代碼託管在同一地點。
  • curl啓用allow_url_fopen在本地主機都在服務器phpinfo()

編輯:

  • 它使用其他網站的頁,而不是我的網站時,工作正常。

  • 該網站的目標頁面完美載入我的瀏覽器。

我的網站的網頁加載速度快像往常一樣,但是當我用curlfile_get_contents,它的速度太慢,甚至無法得到輸出。

+0

檢查你的服務器phpinfo()curl support = enabled和allow_url_fopen = on – 2014-09-19 09:59:16

+0

確定主機/服務器上的'allow_url_fopen = on'? – davidkonrad 2014-09-19 10:01:31

+0

curl已啓用且allow_url_fopen已啓用 – 2014-09-19 10:06:43

回答

2

我認爲你有DNS解決問題。

有兩種方法可以使用您的網站的locahost而不是外部域名。

1.如果你有自己的服務器/ VPS /專用服務器, 在vim /etc/hosts爲127.0.0.1 website.net添加條目或嘗試獲取本地主機與(127.0.0.1)的內容。

2。如果您使用的共享主機,然後嘗試在你的代碼中使用下面url(s)

http://localhost.mywebsite.net/~username/home.php. 
OR 
Try to call http://localhost.mywebsite.net/home.php 

嘗試使用此方法獲取URL內容 -

$dynamic = TRY_ABOVE_URL(S) 
$out = "home.html" ; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$dynamic); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$file = curl_exec($ch); 

if($file === false) { 
    echo 'Curl error: ' . curl_error($ch); 
} 

file_put_contents($out, $file); 
+1

PHP錯誤'警告:file_put_contents(.... html內容.. 。在行file_put_contents($ result ['content'],$ out);' – 2014-09-25 12:32:08

0

調查爲何不活的服務器上工作的原因吧,試試這個:

$dynamic = "http://website.net/home.php"; 

$out  = "home.html" ; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $dynamic); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$file = curl_exec($ch); 

if($file === false) 
{ 
    echo 'Curl error: ' . curl_error($ch); 
} 

curl_close($ch); 

file_put_contents($out, $file); 
+0

apul .. doensn't working :( – 2014-09-19 13:16:25

+0

你能夠通過curl_error函數看到任何錯誤嗎?在腳本的開頭添加<?php ini_set('display_errors',1); error_reporting(E_ALL);?> – 2014-09-19 16:25:30

+0

否錯誤,沒有警告,輸出文件0字節.. – 2014-09-20 19:03:12

0

我認爲這取決於提供SSL配置。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_SSLVERSION, 3); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 

檢查知道smth。關於您的提供商的SSL配置。

+0

我不認爲這取決於提供商SSL配置 – 2014-09-25 11:36:41

0

試試這個:

file_put_contents("home.html", fopen("http://website.net/home.php", 'r')); 

但捲曲也應該起作用。如果您可以通過SSH訪問服務器,請嘗試使用ping或其他方式解析您的域名。可能存在本地DNS問題 - 這將解釋爲什麼您可以從外部域下載。

對於我的示例,您需要allow_fopen_url爲開,所以請首先通過phpinfo()驗證。

+0

頁面加載和加載,幾分鐘後它說'連接超時在file.p在xx線上的hp。 – 2014-09-25 13:06:49

+0

並在xx線上您有file_put_contents ...?我認爲你的麻煩是由DNS問題引起的。你能做'echo gethostbyname(「yourdomainname.com」);'並將它與你的網站翻譯成的實際IP相匹配嗎? – 2014-09-25 14:22:12

+0

它返回服務器的IP地址,並且它匹配。 – 2014-09-25 14:34:08

0

看起來這是一個網絡路由問題,基本上服務器無法找到到網站(本身)的路線。這是一個服務器問題,而不是PHP問題。最快的解決方案是編輯服務器上的hosts文件並將website.net設置爲127.0.0.1。

在Linux服務器上,您將需要添加下面一行到/ etc/hosts文件的底部:

127.0.0.1 website.net 

或者你可以嘗試獲取http://127.0.0.1/home.php,但如果你有多個虛擬主機將無法工作服務器。

+0

該php腳本在'http:// website.net'本身託管..它不使用外部網站 – 2014-09-25 13:04:58

+0

爲什麼服務器無法訪問本身 – 2014-09-25 14:21:30

+0

但在共享服務器編輯主機文件是不可能的 – 2014-09-25 14:34:40

相關問題