Jump to content

url content fetching from php


karthikjayraj

Recommended Posts

Hi ,

 

i am working on a PHP code that would fetch me the" title and the description" from another web page something similar to www.Digg.com .After searching a couple of Open source codes developed by some generous coder I found this piece of code:

 

I have tested this code and it works fine, but the confusion here is it only works from external internet and Iam not able to extract the contents when i use the same piece of code from my office intranet(Our intranet has full access to the internet ) and the best part is when i use the same url for fetching content from Digg.com from my office intranet it works fine.

 

The error I get is file_get_contents time out after 60 seconds

OR

I get a blank page result without any content

 

But the same piece of code when used outside of my intranet works perfectly fine.

 

Please let me know if you find anything incorrect!

 

Thanks

 

<?php

if(isset($_POST['submitlink'])) 
{
function getMetaTitle($content){
  $pattern = "|<[\s]*title[\s]*>([^<]+)<[\s]*/[\s]*title[\s]*>|Ui";
  if(preg_match($pattern, $content, $match))
	return $match[1];
  else
	return false;
}

$url = $_POST['submitlink'];

$data = array();

// get url title
$content = @file_get_contents($url);
$data['title'] = getMetaTitle($content);

// get url description from meta tag
$tags = @get_meta_tags($url);
$data['description'] = $tags['description'];

$dom = new domDocument;

@$dom->loadHTML($content);

$dom->preserveWhiteSpace = false;

$images = $dom->getElementsByTagName('img');

foreach($images as $img)
{
	$imgurl = $img->getAttribute('src');       
	$alt = $img->getAttribute('alt');
	//$size = getimagesize($imgurl);		
	//echo "Title: $alt<br>";
	//echo $imgurl;
	//echo '<img src="'.$imgurl.'"  title="'.$alt.'" width="50px"/>';


}

$formdata .= '<table><tr><td>Title:</td><td><a href=';
$formdata .= 'gethome.php?url=';
$formdata .= $url;
$formdata .= '>';
$formdata .= json_encode($data['title']);
$formdata .= '</a></td></tr>';
$formdata .= '<tr><td>Description:</td><td> ';
$formdata .= json_encode($data['description']);
$formdata .= '</td></tr></table>';
}

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.