This section we come to implement an online http://www.php.cn/css/css-rwd-images.html "target=" _blank "> Image search Small function, it sounds very big on it?" Of course not we want to implement a picture search engine, but to stand on the shoulders of giants, with the help of the API implementation. Our goal, of course, is to learn PHP development!
First create the HTML page, the page simple implementation of input and submit search function:
The index. HTML code is as follows:
The data is post to the search.php interface, so you need to implement search.php:
<?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: Your Apikey ', ); Add Apikey to Header curl_setopt ($ch, Curlopt_httpheader , $header); curl_setopt ($ch, Curlopt_returntransfer, 1); Execute HTTP request 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);//browser jumps to the image URL $redirect = "location:". $url; header ($redirect); >
Image search Here we call the text Search API, see Baidu
API stroe:http://apistore.baidu.com/apiworks/servicedetail/1557.html
is a free API, but there is a limit to the number of calls. We only need to apply for Apikey fill in the above code can be used normally.
Demo:
Click Search, see my big Wuhan!