Home > Backend Development > PHP Tutorial > Getting Started with PHP Development-Online Image Search Code Sharing

Getting Started with PHP Development-Online Image Search Code Sharing

黄舟
Release: 2023-03-06 20:56:02
Original
2941 people have browsed it

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>
Copy after login

Data post to search.php

Interface, so search.php needs to be implemented: ##

<?php
    $ch = curl_init();
    $searchText = $_POST[&#39;content&#39;];
    $url = &#39;http://apis.baidu.com/image_search/search/search?word=&#39;.urlencode($searchText).&#39;&pn=0&rn=1&ie=utf-8&#39;;
	var_dump($url);
    $header = array(
        &#39;apikey: 你的apikey&#39;,
    );
    //添加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);
?>
Copy after login

Here we call the text image search API for image search , see Baidu

API stroe:http://apistore.baidu.com/apiworks/servicedetail/1557.html
Copy after login
is a free API, but there is a limit on the number of calls. We only need to apply for the apikey and fill in the above code to use it normally.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template