Home > Backend Development > PHP Tutorial > Teach you step by step how to do a keyword matching project (search engine) ---- Day 8, teach you how to do it on the eighth day_PHP Tutorial

Teach you step by step how to do a keyword matching project (search engine) ---- Day 8, teach you how to do it on the eighth day_PHP Tutorial

WBOY
Release: 2016-07-13 10:21:00
Original
833 people have browsed it

Teach you step-by-step how to do keyword matching projects (search engines)----The eighth day, teach you how to do the eighth day

The eighth day

Since Xiao Shuai Shuai walked into the misty forest of Taobao Open Platform, he felt that the misty forest was so big, just when he was at a loss.

A pleasant voice sounded, and the sweet voice said: Dear, do you want to pass through this forest? I will guide you forward.

When Xiao Shuaishuai heard that feeling, his body involuntarily followed the sound, and suddenly a bright light flashed, ah...

Xiao Shuai Shuai woke up with a start. When Xiao Shuai Shuai looked at the time, I was surprised that it was so late. In this way, Xiao Shuai Shuai was late for the first time in his career.

Actually, Xiao Shuai Shuai spent a whole night thinking about it on the platform, but he didn’t really figure out anything during the whole night.

Just when I was about to arrive at the company, my cell phone rang. I saw it was Boss Yu and answered the call.

Boss Yu greets me: Good morning, Xiao Shuai Shuai. When will you arrive at the company?

Xiao Shuai Shuai replied: Boss Yu, I’m sorry, I was too busy studying the Taobao open platform last night and overslept this morning. But I’m almost at the company....

When Boss Yu heard this, he was embarrassed to blame Xiaoshuai, so he had to say: Thank you for your hard work, pay attention to rest, and learn to combine work and rest...

Xiao Shuai Shuai, back to: Okay, thank you Boss Yu for your teaching, I’ll just hang up if it’s okay. . (0Afraid of the boss’ sugar-coated bullets0)

After Xiao Shuai Shuai returned to the company, Boss Yu gave Xiao Shuai Shuai a compiled Topclient and asked him to study it. It seemed that Xiao Shuai Shuai was still willing to study the code and let him see the open platform. I really can't see anything.

Taobao API documentation: http://open.taobao.com/api/api_cat_detail.htm?spm=a219a.7386789.0.0.AjaroV&cat_id=4&category_id=102

Topclient comes from Taobao SDK, just slightly modified to remove some framework dependencies. The source code is:

<?<span>php

</span><span>class</span><span> TopClient
{
    </span><span>public</span> <span>$appkey</span><span>;

    </span><span>public</span> <span>$secretKey</span><span>;

    </span><span>public</span> <span>$gatewayUrl</span> = "http://gw.api.taobao.com/router/rest"<span>;

    </span><span>public</span> <span>$format</span> = "json"<span>;

    </span><span>/*</span><span>* 是否打开入参check*</span><span>*/</span>
    <span>public</span> <span>$checkRequest</span> = <span>true</span><span>;

    </span><span>protected</span> <span>$signMethod</span> = "md5"<span>;

    </span><span>protected</span> <span>$apiVersion</span> = "2.0"<span>;

    </span><span>protected</span> <span>$sdkVersion</span> = "top-sdk-php-20110929"<span>;

    </span><span>protected</span> <span>function</span> generateSign(<span>$params</span><span>)
    {
        </span><span>ksort</span>(<span>$params</span><span>);

        </span><span>$stringToBeSigned</span> = <span>$this</span>-><span>secretKey;
        </span><span>foreach</span> (<span>$params</span> <span>as</span> <span>$k</span> => <span>$v</span><span>) {
            </span><span>if</span> ("@" != <span>substr</span>(<span>$v</span>, 0, 1<span>)) {
                </span><span>$stringToBeSigned</span> .= "<span>$k$v</span>"<span>;
            }
        }
        </span><span>unset</span>(<span>$k</span>, <span>$v</span><span>);
        </span><span>$stringToBeSigned</span> .= <span>$this</span>-><span>secretKey;

        </span><span>return</span> <span>strtoupper</span>(<span>md5</span>(<span>$stringToBeSigned</span><span>));
    }

    </span><span>protected</span> <span>function</span> curl(<span>$url</span>, <span>$postFields</span> = <span>null</span><span>)
    {
        </span><span>$ch</span> =<span> curl_init();
        curl_setopt(</span><span>$ch</span>, CURLOPT_URL, <span>$url</span><span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_FAILONERROR, <span>false</span><span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>true</span><span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_CONNECTTIMEOUT, 15<span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_TIMEOUT, 300<span>);
        curl_setopt(</span><span>$ch</span>, CURLOPT_FOLLOWLOCATION, 1<span>);


        </span><span>if</span> (<span>is_array</span>(<span>$postFields</span>) && 0 < <span>count</span>(<span>$postFields</span><span>)) {
            </span><span>$postBodyString</span> = ""<span>;
            </span><span>$postMultipart</span> = <span>false</span><span>;
            </span><span>foreach</span> (<span>$postFields</span> <span>as</span> <span>$k</span> => <span>$v</span><span>) {
                </span><span>if</span> ("@" != <span>substr</span>(<span>$v</span>, 0, 1)) <span>//</span><span>判断是不是文件上传</span>
<span>                {
                    </span><span>$postBodyString</span> .= "<span>$k</span>=" . <span>urlencode</span>(<span>$v</span>) . "&"<span>;
                } </span><span>else</span> <span>//</span><span>文件上传用multipart/form-data,否则用www-form-urlencoded</span>
<span>                {
                    </span><span>$postMultipart</span> = <span>true</span><span>;
                }
            }
            </span><span>unset</span>(<span>$k</span>, <span>$v</span><span>);
            curl_setopt(</span><span>$ch</span>, CURLOPT_POST, <span>true</span><span>);
            </span><span>if</span> (<span>$postMultipart</span><span>) {
                curl_setopt(</span><span>$ch</span>, CURLOPT_POSTFIELDS, <span>$postFields</span><span>);
            } </span><span>else</span><span> {
                curl_setopt(</span><span>$ch</span>, CURLOPT_POSTFIELDS, <span>substr</span>(<span>$postBodyString</span>, 0, -1<span>));
            }
        }
        </span><span>$reponse</span> = curl_exec(<span>$ch</span><span>);

        </span><span>if</span> (curl_errno(<span>$ch</span><span>)) {
            </span><span>throw</span> <span>new</span> <span>Exception</span>(curl_error(<span>$ch</span>), 0<span>);
        } </span><span>else</span><span> {
            </span><span>$httpStatusCode</span> = curl_getinfo(<span>$ch</span>,<span> CURLINFO_HTTP_CODE);
            </span><span>if</span> (200 !== <span>$httpStatusCode</span><span>) {
                </span><span>throw</span> <span>new</span> <span>Exception</span>(<span>$reponse</span>, <span>$httpStatusCode</span><span>);
            }
        }
        curl_close(</span><span>$ch</span><span>);
        </span><span>return</span> <span>$reponse</span><span>;
    }

    </span><span>protected</span> <span>function</span> logCommunicationError(<span>$apiName</span>, <span>$requestUrl</span>, <span>$errorCode</span>, <span>$responseTxt</span><span>)
    {
        </span><span>$localIp</span> = <span>isset</span>(<span>$_SERVER</span>["SERVER_ADDR"]) ? <span>$_SERVER</span>["SERVER_ADDR"] : "CLI"<span>;

        </span><span>$logData</span> = "NAME:<span>$apiName</span>,KEY:<span>$this</span>->appkey,IP:<span>$localIp</span>,URL:<span>$requestUrl</span>,CODE:<span>$errorCode</span>,MSG:" . <span>str_replace</span>("\n", "", <span>$responseTxt</span><span>);

        </span><span>$file</span> = <span>fopen</span>('taobao.api.error.log','a+'<span>);
        </span><span>fwrite</span>(<span>$file</span>,<span>$logData</span><span>);
        </span><span>fclose</span>(<span>$file</span><span>);
    }

    </span><span>public</span> <span>function</span> execute(<span>$request</span>, <span>$session</span> = <span>null</span>, <span>$need_replace</span> = <span>false</span><span>)
    {
        </span><span>if</span> (<span>$this</span>-><span>checkRequest) {
            </span><span>try</span><span> {
                </span><span>$request</span>-><span>check();
            } </span><span>catch</span> (<span>Exception</span> <span>$e</span><span>) {
                </span><span>$result</span> = <span>new</span><span> stdClass();
                </span><span>$result</span>->code = <span>$e</span>-><span>getCode();
                </span><span>$result</span>->msg = <span>$e</span>-><span>getMessage();
                </span><span>return</span> <span>$result</span><span>;
            }
        }
        </span><span>//</span><span>组装系统参数</span>
        <span>$sysParams</span>["v"] = <span>$this</span>-><span>apiVersion;
        </span><span>$sysParams</span>["format"] = <span>$this</span>-><span>format;
        </span><span>$sysParams</span>["method"] = <span>$request</span>-><span>getApiMethodName();
        </span><span>$sysParams</span>["app_key"] = <span>$this</span>-><span>appkey;
        </span><span>$sysParams</span>["timestamp"] = <span>date</span>("Y-m-d H:i:s"<span>);
        </span><span>$sysParams</span>["partner_id"] = <span>$this</span>-><span>sdkVersion;
        </span><span>$sysParams</span>["sign_method"] = <span>$this</span>-><span>signMethod;

        </span><span>if</span> (<span>null</span> != <span>$session</span><span>) {

            </span><span>$sysParams</span>["session"] = <span>$session</span><span>;
        }

        </span><span>//</span><span>获取业务参数</span>
        <span>$apiParams</span> = <span>$request</span>-><span>getApiParas();

        </span><span>//</span><span>签名</span>
        <span>$sysParams</span>["sign"] = <span>$this</span>->generateSign(<span>array_merge</span>(<span>$apiParams</span>, <span>$sysParams</span><span>));

        </span><span>//</span><span>系统参数放入GET请求串</span>
        <span>$requestUrl</span> = <span>$this</span>->gatewayUrl . "?"<span>;
        </span><span>foreach</span> (<span>$sysParams</span> <span>as</span> <span>$sysParamKey</span> => <span>$sysParamValue</span><span>) {
            </span><span>$requestUrl</span> .= "<span>$sysParamKey</span>=" . <span>urlencode</span>(<span>$sysParamValue</span>) . "&"<span>;
        }

        </span><span>$requestUrl</span> = <span>substr</span>(<span>$requestUrl</span>, 0, -1<span>);

        </span><span>//</span><span>发起HTTP请求</span>
        <span>try</span><span> {
            </span><span>$resp</span> = <span>$this</span>->curl(<span>$requestUrl</span>, <span>$apiParams</span><span>);
        } </span><span>catch</span> (<span>Exception</span> <span>$e</span><span>) {
            </span><span>$this</span>->logCommunicationError(<span>$sysParams</span>["method"], <span>$requestUrl</span>, "HTTP_ERROR_" . <span>$e</span>->getCode(), 'RETRY:' . <span>$e</span>-><span>getMessage());
            </span><span>$result</span> = <span>new</span><span> stdClass();
            </span><span>$result</span>->code = <span>$e</span>-><span>getCode();
            </span><span>$result</span>->msg = <span>$e</span>-><span>getMessage();
            </span><span>return</span> <span>$result</span><span>;
        }

        </span><span>//</span><span>解析TOP返回结果</span>
        <span>$respWellFormed</span> = <span>false</span><span>;
        </span><span>if</span> ("json" == <span>$this</span>-><span>format) {
            </span><span>if</span> (<span>$need_replace</span><span>) {
                </span><span>$resp</span> = <span>preg_replace</span>('/[\r\n]+/', '', <span>$resp</span><span>);
            }
            </span><span>$respObject</span> = json_decode(<span>$resp</span><span>);

            </span><span>if</span> (<span>null</span> !== <span>$respObject</span><span>) {
                </span><span>$respWellFormed</span> = <span>true</span><span>;
                </span><span>foreach</span> (<span>$respObject</span> <span>as</span> <span>$propKey</span> => <span>$propValue</span><span>) {
                    </span><span>$respObject</span> = <span>$propValue</span><span>;
                }
            }
        } </span><span>else</span> <span>if</span> ("xml" == <span>$this</span>-><span>format) {
            </span><span>$respObject</span> = @<span>simplexml_load_string</span>(<span>$resp</span><span>);
            </span><span>if</span> (<span>false</span> !== <span>$respObject</span><span>) {
                </span><span>$respWellFormed</span> = <span>true</span><span>;
            }
        }

        </span><span>//</span><span>返回的HTTP文本不是标准JSON或者XML,记下错误日志</span>
        <span>if</span> (<span>false</span> === <span>$respWellFormed</span><span>) {
            </span><span>$this</span>->logCommunicationError(<span>$sysParams</span>["method"], <span>$requestUrl</span>, "HTTP_RESPONSE_NOT_WELL_FORMED", <span>$resp</span><span>);
            </span><span>$result</span> = <span>new</span><span> stdClass();
            </span><span>$result</span>->code = 0<span>;
            </span><span>$result</span>->msg = "HTTP_RESPONSE_NOT_WELL_FORMED"<span>;
            </span><span>return</span> <span>$result</span><span>;
        }

        </span><span>return</span> <span>$respObject</span><span>;
    }
}</span>
Copy after login

Taobao baby request category:

<?<span>php

</span><span>/*</span><span>*
 * TOP API: taobao.item.get request
 *
 * @author auto create
 * @since 1.0, 2011-09-29 15:36:21
 </span><span>*/</span>
<span>class</span><span> ItemGetRequest
{
    </span><span>/*</span><span>*
     * 需要返回的商品对象字段。可选值:Item商品结构体中所有字段均可返回;多个字段用&ldquo;,&rdquo;分隔。如果想返回整个子对象,那字段为item_img,如果是想返回子对象里面的字段,那字段为item_img.url。新增返回字段:second_kill(是否秒杀商品)、auto_fill(代充商品类型),props_name(商品属性名称)
     *</span><span>*/</span>
    <span>private</span> <span>$fields</span><span>;

    </span><span>/*</span><span>*
     * 商品数字ID
     *</span><span>*/</span>
    <span>private</span> <span>$numIid</span><span>;

    </span><span>private</span> <span>$apiParas</span> = <span>array</span><span>();

    </span><span>public</span> <span>function</span> setFields(<span>$fields</span><span>)
    {
        </span><span>$this</span>->fields = <span>$fields</span><span>;
        </span><span>$this</span>->apiParas["fields"] = <span>$fields</span><span>;
    }

    </span><span>public</span> <span>function</span><span> getFields()
    {
        </span><span>return</span> <span>$this</span>-><span>fields;
    }

    </span><span>public</span> <span>function</span> setNumIid(<span>$numIid</span><span>)
    {
        </span><span>$this</span>->numIid = <span>$numIid</span><span>;
        </span><span>$this</span>->apiParas["num_iid"] = <span>$numIid</span><span>;
    }

    </span><span>public</span> <span>function</span><span> getNumIid()
    {
        </span><span>return</span> <span>$this</span>-><span>numIid;
    }

    </span><span>public</span> <span>function</span><span> getApiMethodName()
    {
        </span><span>return</span> "taobao.item.get"<span>;
    }

    </span><span>public</span> <span>function</span><span> getApiParas()
    {
        </span><span>return</span> <span>$this</span>-><span>apiParas;
    }

    </span><span>public</span> <span>function</span><span> check()
    {
        RequestCheckUtil</span>::checkNotNull(<span>$this</span>->fields, "fields"<span>);
        RequestCheckUtil</span>::checkNotNull(<span>$this</span>->numIid, "numIid"<span>);
        RequestCheckUtil</span>::checkMinValue(<span>$this</span>->numIid, 1, "numIid"<span>);
    }
}</span>
Copy after login

Data integrity detection class

<?<span>php
</span><span>/*</span><span>*
 * API入参静态检查类
 * 可以对API的参数类型、长度、最大值等进行校验
 *
 *</span><span>*/</span>
<span>class</span><span> RequestCheckUtil
{
    </span><span>/*</span><span>*
     * 校验字段 fieldName 的值$value非空
     *
     *</span><span>*/</span>
    <span>public</span> <span>static</span> <span>function</span> checkNotNull(<span>$value</span>,<span>$fieldName</span><span>) {
        
        </span><span>if</span>(self::checkEmpty(<span>$value</span><span>)){
            </span><span>throw</span> <span>new</span> <span>Exception</span>("client-check-error:Missing Required Arguments: " .<span>$fieldName</span> , 40<span>);
        }
    }

    </span><span>/*</span><span>*
     * 检验字段fieldName的值value 的长度
     *
     *</span><span>*/</span>
    <span>public</span> <span>static</span> <span>function</span> checkMaxLength(<span>$value</span>,<span>$maxLength</span>,<span>$fieldName</span><span>){        
        </span><span>if</span>(!self::checkEmpty(<span>$value</span>) && <span>strlen</span>(<span>$value</span>) > <span>$maxLength</span><span>){
            </span><span>throw</span> <span>new</span> <span>Exception</span>("client-check-error:Invalid Arguments:the length of " .<span>$fieldName</span> . " can not be larger than " . <span>$maxLength</span> . "." , 41<span>);
        }
    }

    </span><span>/*</span><span>*
     * 检验字段fieldName的值value的最大列表长度
     *
     *</span><span>*/</span>
    <span>public</span> <span>static</span> <span>function</span> checkMaxListSize(<span>$value</span>,<span>$maxSize</span>,<span>$fieldName</span><span>) {    

        </span><span>if</span>(self::checkEmpty(<span>$value</span><span>))
            </span><span>return</span><span> ;

        </span><span>$list</span>=<span>split</span>(",",<span>$value</span><span>);
        </span><span>if</span>(<span>count</span>(<span>$list</span>) > <span>$maxSize</span><span>){
                </span><span>throw</span> <span>new</span> <span>Exception</span>("client-check-error:Invalid Arguments:the listsize(the string split by \",\") of ". <span>$fieldName</span> . " must be less than " . <span>$maxSize</span> . " ." , 41<span>);
        }
    }

    </span><span>/*</span><span>*
     * 检验字段fieldName的值value 的最大值
     *
     *</span><span>*/</span>
    <span>public</span> <span>static</span> <span>function</span> checkMaxValue(<span>$value</span>,<span>$maxValue</span>,<span>$fieldName</span><span>){    

        </span><span>if</span>(self::checkEmpty(<span>$value</span><span>))
            </span><span>return</span><span> ;

        self</span>::checkNumeric(<span>$value</span>,<span>$fieldName</span><span>);

        </span><span>if</span>(<span>$value</span> > <span>$maxValue</span><span>){
                </span><span>throw</span> <span>new</span> <span>Exception</span>("client-check-error:Invalid Arguments:the value of " . <span>$fieldName</span> . " can not be larger than " . <span>$maxValue</span> ." ." , 41<span>);
        }
    }

    </span><span>/*</span><span>*
     * 检验字段fieldName的值value 的最小值
     *
     *</span><span>*/</span>
    <span>public</span> <span>static</span> <span>function</span> checkMinValue(<span>$value</span>,<span>$minValue</span>,<span>$fieldName</span><span>) {
        
        </span><span>if</span>(self::checkEmpty(<span>$value</span><span>))
            </span><span>return</span><span> ;

        self</span>::checkNumeric(<span>$value</span>,<span>$fieldName</span><span>);
        
        </span><span>if</span>(<span>$value</span> < <span>$minValue</span><span>){
                </span><span>throw</span> <span>new</span> <span>Exception</span>("client-check-error:Invalid Arguments:the value of " . <span>$fieldName</span> . " can not be less than " . <span>$minValue</span> . " ." , 41<span>);
        }
    }

    </span><span>/*</span><span>*
     * 检验字段fieldName的值value是否是number
     *
     *</span><span>*/</span>
    <span>protected</span> <span>static</span> <span>function</span> checkNumeric(<span>$value</span>,<span>$fieldName</span><span>) {
        </span><span>if</span>(!<span>is_numeric</span>(<span>$value</span><span>))
            </span><span>throw</span> <span>new</span> <span>Exception</span>("client-check-error:Invalid Arguments:the value of " . <span>$fieldName</span> . " is not number : " . <span>$value</span> . " ." , 41<span>);
    }

    </span><span>/*</span><span>*
     * 校验$value是否非空
     *  if not set ,return true;
     *    if is null , return true;
     *    
     *
     *</span><span>*/</span>
    <span>public</span> <span>static</span> <span>function</span> checkEmpty(<span>$value</span><span>) {
        </span><span>if</span>(!<span>isset</span>(<span>$value</span><span>))
            </span><span>return</span> <span>true</span><span> ;
        </span><span>if</span>(<span>$value</span> === <span>null</span><span> )
            </span><span>return</span> <span>true</span><span>;
        </span><span>if</span>(<span>trim</span>(<span>$value</span>) === ""<span>)
            </span><span>return</span> <span>true</span><span>;
        
        </span><span>return</span> <span>false</span><span>;
    }
}</span>
Copy after login

From Taobao SDK, not original. Slightly revised.

Xiao Shuaishuai took it and looked at it. It was a sacred book again, which was tragic. . Keep researching. .

Just like that, Xiao Shuai Shuai hid in the mountains again to practice the Nine Yin Scriptures.

What is the Taobao program [[[Only 148 yuan in the last three days. Fashion Taoke--Haodian 8 program group purchase (store navigation + API program + SEO Friendly Article Department

Wangdao SEO optimization software works on search engine optimization. My personal understanding of search engine optimization is that SEO creates and uses keyword optimization to help small and medium-sized enterprises’ websites rank higher in search engine query results to obtain the most effective user clicks. . It is based on the retrieval characteristics of search engines for web pages, so that the basic elements of website construction are suitable for the retrieval principles of search engines, so that the search engines can include as many web pages as possible and rank high in the natural retrieval results of search engines, ultimately reaching the website Promotional purposes. The main work of Wangdao search engine optimization is to optimize web content by understanding how various search engines crawl Internet pages, how to index them, and how to determine their ranking of search results for a specific keyword. , a technology that makes it in line with users' browsing habits, improves search engine rankings without damaging user experience, thereby increasing website visits, and ultimately improving the website's sales or publicity capabilities. The so-called "search engine optimization for Wangdao" is to make the website more easily accepted by search engines.

Who is the Hengshui agent of 360 search engine?

360 search engine has agents in Hebei

What are the advantages of 360 promotion

Rich resources - covering many traffic entrances such as 360 navigation, 360 search, etc., with rich display locations and display forms to meet the needs of enterprises Diverse needs. Simple operation - humanized design, fast delivery process, simple and flexible operation, you only need to open an account to achieve self-service delivery. Precise positioning - through keyword matching and delivery by time period, region, and user group, we can accurately target the target user group and display more accurate promotional information for the enterprise. Intelligent and efficient - professional statistical methods and various data reports ensure the scientificity and rigor of 360 promotion; only charging is based on results, truly achieving lower investment and higher returns!
360 search Hebei area processing: 1111

Teach you step by step how to do a keyword matching project (search engine) ---- Day 8, teach you how to do it on the eighth day_PHP Tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/862698.htmlTechArticleTeach you step by step how to do keyword matching projects (search engines)----On the eighth day, teach you how to do the third On the eighth day of the eight days, Xiao Shuai Shuai has walked into the misty forest of Taobao Open Platform, and he feels so...
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