BCS c++ Return Web Page Title


Occasionally requirements exists to return the title of a web page. This utility completes the assigned task by employing Curl, Boost a couple of custom engineered routines.
[codesyntax lang=”cpp”]

string getZTitle(string url) {
	string retVal;
	curl_global_init (CURL_GLOBAL_ALL);
	int ii;
	/*
	 * We use Curl to fetch the web page per the URL.
	 *
	 * A string stream is used to hold the web page
	 *
	 */
	std::ostringstream oss;
	if (CURLE_OK == curl_read(url, oss)) {
		// Web page successfully written to string
		string html;
		html = oss.str();
		/*
		 * The Boost tokenizer is used to parse the string stream into lines of code
		 */
		ii = strToLine(html);
		/*
		 * This routine reads the lines of code until the <Title> tag is encountered.
		 *
		 * The line containing the title tag is trimmed on both sides.
		 *
		 * The HTML tags are discarded leaving us with the title for the web page.
		 *
		 * Retval contains the web pate title.
		 *
		 */
		retVal = getTitle(ii);
	}
	curl_global_cleanup();
	return retVal;
}

[/codesyntax]
Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

Leave a Reply

Your email address will not be published. Required fields are marked *