How to Get Lyrics from bing.com (Bing and Sing) using PHP Code

Authors: CodeToday | PHP Code | Views: 46 | Posted: 03 AM: 10/12/2017

Bing.com Starts Showing Full Song Lyrics Right In search results with more than 500,000 famous and familiar lyrics. You do not need to visit any other site because the lyrics are fully displayed by Bing Search. But how can you put lyrics on your website without having to copy and paste.

Today I will guide you how to get lyrics from bing search to your website using PHP Code.

After searching for the lyrics for "love story lyrics" we see the results as the image below from bing.com

search result get lyrics

The steps are as follows

  • Create html form: receive lyrics search data
  • Create a query path to bing.com
  • Get the data and analyze the html structure to find the lyrics
  • Show Lyrics on your website or application

Lyrics search path on bing.com

Note: When searching for lyrics, in addition to the song title you must add the suffix "Lyrics"
https://www.bing.com/search?q='.urlencode($_POST['song']);

HTML FORM

<form action="" method="post" class="form-inline">
<div class="form-group">
<input type="text" name="song" value="<?=$_POST['song']?>" size="80" class="form-control" required/>
</div>
<div class="form-group">
<button class="btn btn-info" type="submit" name="submit"><i class="glyphicon glyphicon-search"></i> Get Lyrics Now</button>
</div>
</form>

PHP Full Source Code

<?
if(isset($_POST['submit'])) {

	$song      = trim($_POST['song']);
	echo '<h3 class = "text-primary">'.$song.'</h3>';
	$urlLyrics =  'https://www.bing.com/search?q='.urlencode($song);	
	$dataSong  = curlGet($urlLyrics);
	preg_match_all('/<p class="b_paractl">(.*?)</p>/si', $dataSong, $text);	
		foreach($text[1] as $lyrics) {
		  echo $lyrics;	
	}
}


function curlGet($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	curl_setopt($ch, CURLOPT_HTTPGET, 1 );
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_URL, $url );
	curl_setopt($ch, CURLOPT_REFERER, $ref );
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}
?>
After completing the above steps, you will get the image as below:

How to Get Lyrics from bing.com (Bing and Sing) using PHP Code


If you have any questions, please leave a message below