In this section we will implement an online Does the small function of image search sound very high-end? Of course, we are not trying to implement an image search engine, but we have to stand on the shoulders of giants and implement it with the help of API. The purpose is, of course, to learn PHP development!
##First create an html page, which simply implements the input and submit search functions:
#index.html code is as follows:
##<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf8"/>
<title>图片搜索</title>
</head>
<body>
<form method="post" action="search.php">
<p>文本搜图</p>
<p>文本搜图:<input type="text" name="content"></p>
<input type="submit" value="搜索">
</form>
</body>
</html>
Interface, so search.php needs to be implemented: ##
<?php $ch = curl_init(); $searchText = $_POST['content']; $url = 'http://apis.baidu.com/image_search/search/search?word='.urlencode($searchText).'&pn=0&rn=1&ie=utf-8'; var_dump($url); $header = array( 'apikey: 你的apikey', ); //添加apikey到header curl_setopt($ch, CURLOPT_HTTPHEADER , $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //执行HTTP请求 curl_setopt($ch , CURLOPT_URL , $url); $res = curl_exec($ch); $result = json_decode($res)->data->ResultArray; var_dump($result); $url = $result[0]->ObjUrl; var_dump($url); //浏览器跳转到图片网址 $redirect = "Location: ".$url; header($redirect); ?>
Here we call the text image search API for image search , see Baidu
API stroe:http://apistore.baidu.com/apiworks/servicedetail/1557.html
Demonstrate:
# #Click to search and see my big Wuhan!
The above is the detailed content of Getting Started with PHP Development-Online Image Search Code Sharing. For more information, please follow other related articles on the PHP Chinese website!