簡體   English   中英

將Facebook發布到網頁時出現超鏈接問題

[英]Issue with hyperlinks when pulling Facebook post into web page

我正在使用以下腳本從我的Facebook頁面提取最新帖子。

它按預期執行此操作,但是,如果Facebook帖子中包含超鏈接,則該鏈接將變為亂碼且不再起作用。 如果可以使用我的代碼,請嘗試一下-確保已安裝curl。

<?php
$url = "http://www.facebook.com/feeds/page.php?id=466171083413035&format=json"; 

// disguises the curl using fake headers and a fake user agent. 
function disguise_curl($url) 
{ 
  $curl = curl_init(); 

  // Setup headers - the same headers from Firefox version 2.0.0.6 
  // below was split up because the line was too long. 
  $header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,"; 
  $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"; 
  $header[] = "Cache-Control: max-age=0"; 
  $header[] = "Connection: keep-alive"; 
  $header[] = "Keep-Alive: 300"; 
  $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; 
  $header[] = "Accept-Language: en-us,en;q=0.5"; 
  $header[] = "Pragma: "; // browsers keep this blank. 

  curl_setopt($curl, CURLOPT_URL, $url); 
  curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla'); 
  curl_setopt($curl, CURLOPT_HTTPHEADER, $header); 
  curl_setopt($curl, CURLOPT_REFERER, ''); 
  curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate'); 
  curl_setopt($curl, CURLOPT_AUTOREFERER, true); 
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($curl, CURLOPT_TIMEOUT, 10); 

  $html = curl_exec($curl); // execute the curl command 
  curl_close($curl); // close the connection 

  return $html; // and finally, return $html 
} 

// uses the function and displays the text off the website 
$text = disguise_curl($url); 

$json_feed_object = json_decode($text);

$i = 0;

foreach ( $json_feed_object->entries as $entry )
{
    echo "<h2>{$entry->title}</h2>";
    $published = date("g:i A F j, Y", strtotime($entry->published));
    echo "<small>{$published}</small>";
    $content = preg_replace("/<img[^>]+\>/i", "", $entry->content);
    echo "<p style='word-wrap:break-word;'>{$content}</p>";
    echo "<hr />";
$i++;
if ($i == 1) { break;}
}
?>

編輯我的超鏈接顯示為: <a href="/l.php?u=http%3A%2F%2Fwww.empireonline.com%2Fnews%2Fstory.asp%3FNID%3D36903&amp;h=AAQFjtw9e&amp;s=1" target="_blank" rel="nofollow nofollow" onmouseover="LinkshimAsyncLink.swap(this, &quot;http:\\/\\/www.empireonline.com\\/news\\/story.asp?NID=36903&quot;);" onclick="LinkshimAsyncLink.swap(this, &quot;\\/l.php?u=http\%3A\%2F\%2Fwww.empireonline.com\%2Fnews\%2Fstory.asp\%3FNID\%3D36903&amp;h=AAQFjtw9e&amp;s=1&quot;);">http://www.empireonline.com/news/story.asp?NID=36903</a><br/><br/><a href="http://www.facebook.com/photo.php?fbid=597077380322404&amp;set=a.583314588365350.145103.466171083413035&amp;type=1&amp;relevant_count=1" id="" title="" target="" onclick="" style=""></a> <a href="/l.php?u=http%3A%2F%2Fwww.empireonline.com%2Fnews%2Fstory.asp%3FNID%3D36903&amp;h=AAQFjtw9e&amp;s=1" target="_blank" rel="nofollow nofollow" onmouseover="LinkshimAsyncLink.swap(this, &quot;http:\\/\\/www.empireonline.com\\/news\\/story.asp?NID=36903&quot;);" onclick="LinkshimAsyncLink.swap(this, &quot;\\/l.php?u=http\%3A\%2F\%2Fwww.empireonline.com\%2Fnews\%2Fstory.asp\%3FNID\%3D36903&amp;h=AAQFjtw9e&amp;s=1&quot;);">http://www.empireonline.com/news/story.asp?NID=36903</a><br/><br/><a href="http://www.facebook.com/photo.php?fbid=597077380322404&amp;set=a.583314588365350.145103.466171083413035&amp;type=1&amp;relevant_count=1" id="" title="" target="" onclick="" style=""></a>

有人遇到過這個問題嗎? 有解決方案嗎?

非常感謝您的指導。

我的錯。

我需要做的是在任何URL上替換字符串並附加facebook.com。

這是我的代碼,以防其他問題:

$content = str_replace(' href="/l.php', ' href="http://www.facebook.com/l.php',$content);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM