>  기사  >  백엔드 개발  >  手把手教你做关键词匹配项目(搜索引擎)---- 第三天,教你做----_PHP教程

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

WBOY
WBOY원래의
2016-07-13 10:21:15871검색

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

第三天

小王(运营总监)看到小丁丁整天都在淘宝、百度、魔方、拍拍上面淘关键词,每天花费的时间好长,工作效率又低,拿着这个借口来找到我。

说到:小帅帅,你看小丁丁每天都在淘宝、百度、魔方、拍拍上面淘关键词花费的时间好长,你能不能帮帮忙,看看能不能让系统自己做啦,这样可以节省好多人力,带来的效益多高。(0 其实就是为了掩饰他们懒惰 0)

小帅帅一听到可以带来的效益好高,王总还求着我呢 ,马上 两眼冒着星光,是该好好体现, 解决这个问题就可以体现出我的价值。

小帅帅拍着胸膛保证到:王总,这个小KS啦,马上帮你办到。 ( 0 程序员就是这么可爱,乐于挑战,乐于接受,从不需要高额的回报 0 )

小王拍了拍小帅帅: 好小伙子,好好加油,等你的好消息。

小帅帅从此乐呵了一整天。从没去想想这是多么蛋疼的一项任务。

关键词来源示例图:

当小帅帅醒过来时,才意识到原来这个项目不是一般的蛋疼啊。

小帅帅不知道如何下手,就屁颠屁颠的跑到小于(技术总监)旁边(0 有技术总监真好,强大的靠山,反正有人背黑锅 0)。

说到:于老大,刚才王总给了这个任务给我,我不晓得怎么办好,能否高抬贵嘴,指导下。

于老大瞄了一眼,说到:你是用php做的吧,php做就复杂点咯,知道curl、html dom解析吗?

小帅帅,说到:不太懂耶,没用过,看起来好高深的样子。

于老大鄙视了一番:这有什么高深的,很简单的,百度一下咯。(老大就是老大,什么事情在他眼里都是简单的,好崇拜的对象)

于老大在baidu搜索框里面输入 php curl,查出了结果让小帅帅去了解。

php curl手册: http://cn2.php.net/manual/zh/book.curl.php

小帅帅查看玩手册后,随手写了一份:

<span>#</span><span>请求淘宝首页</span>

<span>$curl</span> =<span> curl_init();
curl_setopt_array(</span><span>$curl</span>,<span>array</span><span>(
        CURLOPT_FAILONERROR </span>=> <span>false</span>,<span>
        CURLOPT_RETURNTRANSFER </span>=> <span>true</span>,<span>
        CURLOPT_FOLLOWLOCATION </span>=> 1,<span>
        CURLOPT_CONNECTTIMEOUT </span>=> 15,<span>
        CURLOPT_TIMEOUT </span>=> 60,
        <span>//</span><span>CURLOPT_COOKIESESSION => 1,  </span>
        CURLOPT_URL => "http://www.taobao.com"<span>
));

</span><span>$result</span> = curl_exec(<span>$this</span>-><span>curl);
</span><span>echo</span> <span>$result</span>;

小帅帅非常高兴,因为学到了新的知识。小帅帅又犯愁了,内容是取到了,怎么取里面的关键词?

小帅帅又屁颠屁颠的跑到小于(技术总监),去请教。

说道:于老大,curl我已经弄懂了,我取道淘宝的首页了,下面怎么办?

于老大瞄了一眼代码,很不乐意的说道:恩,写是写好了,怎么看着这么别扭。

小帅帅心里就不乐意了,想到我写的明明这么好,又简单使用,怎么别扭了。

于老大就把以前的代码翻出来,随手扔给小帅帅,说到:你先去看懂这份代码,用这份代码重新写一份?

 

文件内容:

<span>/*</span><span>*
 * cURL 常用操作封装
 *
 * @author oShine
 </span><span>*/</span>
<span>class</span><span> ExtendedCurl
{
    </span><span>/*</span><span>*
     * 返回 JSON 内容为对象
     </span><span>*/</span>
    <span>const</span> JSON_OBJECT = 0<span>;

    </span><span>/*</span><span>*
     * 返回 JSON 内容为数组
     </span><span>*/</span>
    <span>const</span> JSON_ARRAY = 1<span>;

    </span><span>/*</span><span>*
     * cURL Handle
     *
     * @var resource
     </span><span>*/</span>
    <span>private</span> <span>$curl</span><span>;

    </span><span>/*</span><span>*
     * 当前(默认) cURL 参数
     *
     * @var array
     </span><span>*/</span>
    <span>private</span> <span>$options</span> = <span>array</span><span>(
        CURLOPT_FAILONERROR </span>=> <span>false</span>,<span>
        CURLOPT_RETURNTRANSFER </span>=> <span>true</span>,<span>
        CURLOPT_FOLLOWLOCATION </span>=> 1,<span>
        CURLOPT_CONNECTTIMEOUT </span>=> 15,<span>
        CURLOPT_TIMEOUT </span>=> 60,
        <span>//</span><span>CURLOPT_COOKIESESSION => 1,        </span>
<span>    );

    </span><span>/*</span><span>*
     * 最后一次请求的错误信息
     *
     * @var null|string
     </span><span>*/</span>
    <span>private</span> <span>$error</span> = <span>null</span><span>;

    </span><span>/*</span><span>*
     * @var int
     </span><span>*/</span>
    <span>private</span> <span>$httpCode</span> = <span>null</span><span>;

    </span><span>/*</span><span>*
     * @param array $defaultOptions
     * @internal param array $options 可选的覆盖默认 cURL 参数
     </span><span>*/</span>
    <span>public</span> <span>function</span> __construct(<span>array</span> <span>$defaultOptions</span> = <span>array</span><span>())
    {
        </span><span>$this</span>->curl =<span> curl_init();
        </span><span>if</span> (!<span>empty</span>(<span>$defaultOptions</span><span>)) {
            </span><span>$this</span>->options = <span>$defaultOptions</span><span>;
        }
    }

    </span><span>/*</span><span>*
     * 设置 cURL 多个选项
     *
     * @param array $options
     </span><span>*/</span>
    <span>public</span> <span>function</span> setOptions(<span>array</span> <span>$options</span><span>)
    {
        </span><span>foreach</span> (<span>$options</span> <span>as</span> <span>$key</span> => <span>$value</span><span>) {
            </span><span>$this</span>->setOption(<span>$key</span>, <span>$value</span><span>);
        }
    }

    </span><span>/*</span><span>*
     * 设置 cURL 单个选项
     *
     * @param $key
     * @param $value
     </span><span>*/</span>
    <span>public</span> <span>function</span> setOption(<span>$key</span>, <span>$value</span><span>)
    {
        </span><span>$this</span>->options[<span>$key</span>] = <span>$value</span><span>;
    }

    </span><span>/*</span><span>*
     * 发送 GET 请求并返回解析后的 JSON 内容
     *
     * @param $url
     * @param array $data
     * @param int $type
     * @return null|object|array
     </span><span>*/</span>
    <span>public</span> <span>function</span> getJson(<span>$url</span>, <span>array</span> <span>$data</span> = <span>array</span>(), <span>$type</span> = self::<span>JSON_ARRAY)
    {
        </span><span>$content</span> = <span>$this</span>->get(<span>$url</span>, <span>$data</span><span>);
        </span><span>return</span> json_decode(<span>$content</span>, <span>$type</span><span>);
    }

    </span><span>/*</span><span>*
     * 发送 GET 请求
     *
     * @param $url
     * @param array $data
     * @return null|string
     </span><span>*/</span>
    <span>public</span> <span>function</span> get(<span>$url</span>, <span>array</span> <span>$data</span> = <span>array</span><span>())
    {
        </span><span>if</span> (!<span>empty</span>(<span>$data</span><span>)) {
            </span><span>if</span> (<span>false</span> === <span>strpos</span>(<span>$url</span>, '?'<span>)) {
                </span><span>$url</span> .= '?'<span>;
            } </span><span>else</span><span> {
                </span><span>$url</span> .= '&'<span>;
            }
            </span><span>$url</span> .= <span>http_build_query</span>(<span>$data</span><span>);
        }
        </span><span>$options</span> = <span>array</span><span>(
            CURLOPT_URL </span>=> <span>$url</span>,<span>
        );
        </span><span>return</span> <span>$this</span>->request(<span>$options</span><span>);
    }

    </span><span>/*</span><span>*
     * 发送 cURL 请求
     *
     * @param array $options
     * @return mixed
     </span><span>*/</span>
    <span>private</span> <span>function</span> request(<span>array</span> <span>$options</span> = <span>array</span><span>())
    {
        </span><span>$this</span>->setOptions(<span>$options</span><span>);
        curl_setopt_array(</span><span>$this</span>->curl, <span>$this</span>-><span>options);
        </span><span>$result</span> = curl_exec(<span>$this</span>-><span>curl);
        </span><span>$errorNo</span> = curl_errno(<span>$this</span>-><span>curl);
        </span><span>$response</span> = curl_getinfo( <span>$this</span>-><span>curl );
        </span><span>if</span> (<span>$errorNo</span><span>) {
            </span><span>$this</span>->error = '[' . <span>$errorNo</span> . '] ' . curl_error(<span>$this</span>-><span>curl);
        } </span><span>else</span><span> {
            </span><span>$this</span>->error = <span>null</span><span>;
        }
        </span><span>if</span>(<span>isset</span>(<span>$response</span>['http_code'<span>])){
            </span><span>$this</span>->httpCode = <span>$response</span>['http_code'<span>];
        }

        </span><span>return</span> <span>$result</span><span>;
    }

    </span><span>/*</span><span>*
     * 发送 POST 请求并返回解析后的 JSON 内容
     *
     * @param $url
     * @param array $data
     * @param int $type
     * @return null|object|array
     </span><span>*/</span>
    <span>public</span> <span>function</span> postJson(<span>$url</span>, <span>array</span> <span>$data</span> = <span>array</span>(), <span>$return</span> = self::<span>JSON_ARRAY)
    {
        </span><span>$content</span> = <span>$this</span>->post(<span>$url</span>, <span>$data</span><span>);
        </span><span>return</span> json_decode(<span>$content</span>, <span>$return</span><span>);
    }

    </span><span>/*</span><span>*
     * 发送 POST 请求
     *
     * @param $url
     * @param array $data
     * @return null|string
     </span><span>*/</span>
    <span>public</span> <span>function</span> post(<span>$url</span>, <span>array</span> <span>$data</span> = <span>array</span><span>())
    {
        </span><span>$options</span> = <span>array</span><span>(
            CURLOPT_URL </span>=> <span>$url</span>,<span>
            CURLOPT_POST </span>=> 1,<span>
        );
        </span><span>if</span> (!<span>empty</span>(<span>$data</span><span>)) {
            </span><span>if</span> (<span>$this</span>->isMultiPart(<span>$data</span><span>)) {
                </span><span>$options</span>[CURLOPT_POSTFIELDS] = <span>$data</span><span>;
            } </span><span>else</span><span> {
                </span><span>$options</span>[CURLOPT_POSTFIELDS] = <span>http_build_query</span>(<span>$data</span><span>);
            }
        }
        </span><span>return</span> <span>$this</span>->request(<span>$options</span><span>);
    }

    </span><span>private</span> <span>function</span> isMultiPart(<span>$data</span><span>)
    {
        </span><span>foreach</span> (<span>$data</span> <span>as</span> <span>$value</span><span>) {
            </span><span>if</span> ('@' == <span>$value</span>[0<span>])
                </span><span>return</span> <span>true</span><span>;
        }
        </span><span>return</span> <span>false</span><span>;
    }

    </span><span>/*</span><span>*
     * 判断最后一次请求是否有错误
     *
     * @return bool
     </span><span>*/</span>
    <span>public</span> <span>function</span><span> hasError()
    {
        </span><span>return</span> <span>null</span> !== <span>$this</span>-><span>error;
    }

    </span><span>/*</span><span>*
     * 获取最后一次请求的错误信息
     *
     * @return null|string
     </span><span>*/</span>
    <span>public</span> <span>function</span><span> getError()
    {
        </span><span>return</span> <span>$this</span>-><span>error;
    }

    </span><span>public</span> <span>function</span><span> getHttpCode()
    {
        </span><span>return</span> <span>$this</span>-><span>httpCode;
    }
} </span>

 

小帅帅心里十分不乐意,心里恨不得要揍揍于老大,但又不得不屈服他的淫威之下,只得答应说到:好的,我先回去琢磨琢磨一下。

小帅帅 从此郁闷了一天。 拿着天书式的武林秘籍去修炼去了。

 

百度搜索引擎中搜索词是如与跟推广关键词匹配的?

现在 百度推广有精确、短语、广泛三种匹配方式,,一般来说黄金组合匹配方式是:广泛匹配+搜索词报告+否定关键词。流量来说的话就是 广泛>短语>精确啦,可以根据你们呢的实际情况进行设置!
 

用搜索引擎搜索时,如果有三个关键词,要精确匹配,该怎输入?

如果要精确匹配,需要ABC都带引号。
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/860058.htmlTechArticle手把手教你做关键词匹配项目(搜索引擎)---- 第三天,教你做---- 第三天 小王(运营总监)看到小丁丁整天都在淘宝、百度、魔方、拍拍...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.