2013-02-24 57 views
0

我有這樣的功能:無法獲取內容的PHP

function get_vk($url) { 
    $str = file_get_contents("http://vk.com/share.php?act=count&index=1&url=" . $url); 
    if (!$str) return 0; 
    return preg_match('/^VK.Share.count\((\d+),\s+(\d+)\);$/i', $rq, $i) ? (int) $i[2] : 0; 
} 

但這個函數總是返回0,因爲$str爲NULL。但是,如果我們只是把這個鏈接

https://vk.com/share.php?act=count&index=1&url=http://stackoverflow.com 

到瀏覽器,它會返回VK.Share.count(1, 43);問題出在哪裏?

+0

所以你的瀏覽器可以取它,但不是你的serveR?開始尋找**爲什麼**服務器不能。你是否因濫用該服務而被列入黑名單?防火牆阻止傳出端口80連接?你的問題現在無法解決。 – 2013-02-24 20:52:42

+0

查看您是否可以從vk.com服務列入黑名單。 – 2013-02-24 20:53:53

+0

在互聯網上都使用這個功能。也許它會與捲曲一起工作? – user2058653 2013-02-24 20:54:33

回答

1

您沒有將輸入字符串傳遞給preg_match。

的代碼應該這樣寫:

function get_vk($url) { 
    $str = file_get_contents("http://vk.com/share.php?act=count&index=1&url=" . $url); 
    if (!$str) return 0; 
    preg_match('/^VK.Share.count\((\d+),\s+(\d+)\);$/i', $str, $matches); 
    $rq = $matches[1]; 

    return $rq; 
} 

echo get_vk('http://stackoverflow.com'); 

您可以在http://php.net/preg_match對語法的preg_match閱讀起來。

+0

如果我的解決方案對你有幫助,你介意點擊複選標記接受答案嗎?謝謝! – 2014-05-21 06:23:13