Heim >php教程 >php手册 >手把手教你做关键词匹配项目(搜索引擎)---- 第八天,教你做第八天

手把手教你做关键词匹配项目(搜索引擎)---- 第八天,教你做第八天

WBOY
WBOYOriginal
2016-06-13 09:27:24954Durchsuche

手把手教你做关键词匹配项目(搜索引擎)---- 第八天,教你做第八天

第八天

话说小帅帅自从走进了淘宝开放平台这个迷雾森林,感觉这迷雾森林好大,正当他无所适从的时候。

一位悦耳动听的声音响起来了,甜甜的声音说道:亲,想通过这片森林吗,我将指引你前进。

小帅帅一听,那种感觉,身体不由自主的跟随这声音而去,突然一道强光闪过,啊.....

小帅帅惊醒了。小帅帅一看时间,我滴个天,这么晚了。就这样小帅帅从业一来第一次迟到。

其实小帅帅在平台里面琢磨了一个晚上,整个晚上其实也没琢磨个啥出来。

正当要到公司的时候,手机的铃声响起来了,一看是于老大的电话,接通电话。

于老大问候到:小帅帅,早啊, 你什么时候到公司丫。

小帅帅答到: 于老大,不好意思丫,昨天晚上研究那个淘宝开放平台,研究太玩了,今早睡过头了。不过我快到公司了....

于老大一听,不好意思责怪小帅帅啥,只好说道:辛苦你了,注意休息,学会劳逸结合...

小帅帅,回到: 好的,谢谢于老大的教诲,没事就挂了哈。。( 0害怕于老大的糖衣炮弹0 )

小帅帅回到公司后,于老大就给了一份整理后的Topclient给小帅帅,让他去研究下,看样子小帅帅还是乐于研究代码,让他看开放平台,还真看不出什么。

淘宝宝贝API文档:http://open.taobao.com/api/api_cat_detail.htm?spm=a219a.7386789.0.0.AjaroV&cat_id=4&category_id=102

Topclient来自Taobao SDK ,只是稍微修正,去掉了一些框架的依赖,源码为:

<?<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>

淘宝宝贝请求类:

<?<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>

数据完整性检测类

<?<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>

来自Taobao SDK,非原创。稍微修订。

小帅帅拿着一看,又是天书,悲剧了。。 继续研究吧。。

就这样小帅帅又躲深山去修炼九阴真经了。

 

什是淘宝客程序【【【最后三天仅148元风尚淘客--好店8程序团购(店铺导航+API程序+SEO友好文章系

旺道SEO优化软件对搜索引擎优化工作的时间,个人理解搜索引擎优化即SEO创造运用关键词优化帮助中小企业的网站能在搜索引擎查询结果中靠前,以获得最有效的用户点击。是针对搜索引擎对网页的检索特点,让网站建设各项基本要素适合搜索引擎的检索原则,从而使搜索引擎收录尽可能多的网页,并在搜索引擎自然检索结果中排名靠前,最终达到网站推广的目的。旺道搜索引擎优化的主要工作是:通过了解各类搜索引擎如何抓取互联网页面、如何进行索引以及如何确定其对某一特定关键词的搜索结果排名等技术,来对网页内容进行相关的优化,使其符合用户浏览习惯,在不损害用户体验的情况下提高搜索引擎排名,从而提高网站访问量,最终提升网站的销售能力或宣传能力的技术。所谓 针对旺道搜寻引擎优化处理 ,是为了要让网站更容易被搜寻引擎接受。
 

360搜索引擎衡水代理商是哪家?

360搜索引擎河北有代理商

360推广有哪些优势

资源丰富——覆盖360导航、360搜索等众多流量入口,丰富的展现位置和展现形式,满足企业的多样化需求。 操作简单——人性化设计,投放流程快捷,操作简单灵活,只需要开通账户即可实现自助式投放。 精准定位——通过关键词匹配,以及分时段、地域、用户群投放,精准锁定目标用户群,为企业展现更精准的推广信息。 智能高效——专业的统计方法和多种数据报告,保证360推广的科学性和严谨性;只按效果收费,真正实现更低投入,更高回报!
  360搜索河北地区办理: 1111


 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn