Getting Started with PHP development-online Image search code sharing

Source: Internet
Author: User
Tags getting started with php learn php
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.