I am getting these two errors when retrieving meta data from a remote webpage. Is this an escaping issue or maybe a cURL issue?
Warning: get_meta_tags(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://...@import url( "http://www.zymic.com/forum/style_images/v6/folder_editor_images/css_rte.css" ); </style> </head> <body> <div id="ipbwrapper"> <!--ipb.javascript.start--> <script type="text/javascript"> //<![CDATA[ var ipb_var_st = "0"; var ipb_lang_tpl_q1 = "Please enter a page number to jump to between 1 and"; var ipb_var_s = "f2e0d2b492f248ec27ef34ae291a1db4"; var ipb_var_phpext = "php"; var ipb_var_base_url = "http://www.zymic.com/forum/index.php?s=f2e0d2b492f248ec27ef34ae291a1db4&"; var ipb_var_image_url = "style_images/v6"; var ipb_input_f = "34"; var ipb_input_t = "5188"; var ipb_input_p = ""; var ipb_var_cookieid
= ""; var ipb_var_cookie_ in public_html/list/main/output.php on line 22 retriev开发者_如何学编程e pagetitle Warning: file_get_contents(<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://...@import url( "http://www.zymic.com/forum/style_images/v6/folder_editor_images/css_rte.css" ); </style> </head> <body> <div id="ipbwrapper"> <!--ipb.javascript.start--> <script type="text/javascript"> //<![CDATA[ var ipb_var_st = "0"; var ipb_lang_tpl_q1 = "Please enter a page number to jump to between 1 and"; var ipb_var_s = "f2e0d2b492f248ec27ef34ae291a1db4"; var ipb_var_phpext = "php"; var ipb_var_base_url = "http://www.zymic.com/forum/index.php?s=f2e0d2b492f248ec27ef34ae291a1db4&"; var ipb_var_image_url = "style_images/v6"; var ipb_input_f = "34"; var ipb_input_t = "5188"; var ipb_input_p = ""; var ipb_var_cookieid
= ""; var ipb_var_coo in /public_html/list/main/output.php on line 27
Here is the code:
////Use Curl Library to get page content for security
$url = 'http://en.wikipedia.org/wiki/Category:Lists_of_lists';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, 'ListBot 1.0: Used for compiling a DB of lists across the internet.');
$str = curl_exec($curl);
curl_close($curl);
//get metadata
$tags = get_meta_tags($str);
//Get page title
function get_page_title($str){
if( !($data = file_get_contents($str)) ) return false;
if( preg_match("#<title>(.+)<\/title>#iU", $data, $t)) {
return trim($t[1]);
} else {
return false;
}
}
///////////
echo('retrieve pagetitle');
$tags['title'] = get_page_title($str);
get_meta_tags expects a file location (commonly a url).
You could request the url directly and parse the headers, but you'd probably get better results doing a regular expression match on the string you retrieved with curl.
You have a nice bit of code that grabs the title. Simply modify that to grab all the meta tags.
In the php.net page describing "get_meta_tags()" jstel at 126 dot com contributed this nice function call:
preg_match_all("/<meta[^>]+(http-equiv|name)=\"([^\"])\"[^>]" . "+content=\"([^\"])\"[^>]*>/i", $v, $split_content[], PREG_PATTERN_ORDER);
Which will search string $v for meta data and dump matches into $split_content. In his sample he does a bunch of looping that seems unneeded, but I'd suggest looking at his code and seeing if you can adapt it.
精彩评论