Use PHP to capture user comments on Taobao products. Picture example, _PHP tutorial

WBOY
Release: 2016-07-12 08:54:48
Original
1025 people have browsed it

Use PHP to capture users’ comments on Taobao products. Picture examples,

Why do you want to do this function? It's because when I was building a Taoke website some time ago, I thought about whether I could capture the buyer's show of Taobao products? After some troubles, it was discovered that Taobao product user evaluation information is retrieved through Ajax. By sniffing the URL, it was found that the request interface for comment data is:

https:<span>//</span><span>rate.tmall.com/list_detail_rate.htm?itemId=524394294771&spuId=341564036&sellerId=100414600&order=3&currentPage=1&append=0&content=1&tagId=&posi=&picture=1&callback=jsonp2339</span>
Copy after login

In fact, many of the above parameters are easy to understand. itemId is the ID of the product, currentPage is the current page, and when picture is 1, it displays reviews with pictures. Since it is capturing the buyer show, the picture parameter must be 1.

If you directly access the above interface, you will get the request result as shown below:

I was shocked when I saw that the request result was in jsonp format. I didn’t know how to parse it, but to change my thinking, it might be a good idea to directly use PHP’s regular expressions to parse it. After trying it, I was able to parse the comments correctly. Content and picture content of buyer show, as shown in the picture:

The effect is good. The code realizes the capture of comment content and the capture of buyer show pictures. Here is the code:

<?<span>php
</span><span>$url</span> = "https://rate.tmall.com/list_detail_rate.htm?itemId=524394294771&spuId=341564036&sellerId=100414600&order=3&currentPage=1&append=0&content=1&tagId=&posi=&picture=1&callback=jsonp2339"<span>;
</span><span>$ch2</span> =<span> curl_init();
curl_setopt(</span><span>$ch2</span>, CURLOPT_URL, <span>$url</span><span>);
curl_setopt(</span><span>$ch2</span>, CURLOPT_FOLLOWLOCATION, <span>TRUE</span><span>);
curl_setopt(</span><span>$ch2</span>, CURLOPT_SSL_VERIFYHOST, <span>FALSE</span><span>);
curl_setopt(</span><span>$ch2</span>, CURLOPT_SSL_VERIFYPEER, <span>false</span><span>);
curl_setopt(</span><span>$ch2</span>, CURLOPT_RETURNTRANSFER, <span>TRUE</span><span>);
</span><span>$texts</span> = curl_exec(<span>$ch2</span><span>);
curl_close(</span><span>$ch2</span><span>);
</span><span>//</span><span>echo $texts;</span>
<span>$pattern</span> = '/"pics"(.+?)","reply"/is'<span>;
</span><span>preg_match_all</span>(<span>$pattern</span>, <span>$texts</span>, <span>$match</span><span>);
</span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>count</span>(<span>$match</span>[0]);<span>$i</span>++<span>){
    </span><span>$pattern2</span> = '/"rateContent":"(.+?)."reply"/is'<span>;
    </span><span>preg_match</span>(<span>$pattern2</span>, <span>$match</span>[0][<span>$i</span>], <span>$matchcomments_only</span><span>);
    </span><span>echo</span> "<p>".<span>str_replace</span>('","rateDate":"',' ',<span>str_replace</span>('","reply"','',<span>str_replace</span>('"rateContent":"','',<span>$matchcomments_only</span>[0])))."</p>"<span>;

    </span><span>$pattern3</span> = '/img.alicdn(.+?).jpg/is'<span>;
    </span><span>preg_match</span>(<span>$pattern3</span>, <span>$match</span>[0][<span>$i</span>], <span>$matchpic_only</span><span>);
    </span><span>echo</span> '<img src="http://'.<span>$matchpic_only</span>[0].'" width=120>'<span>;
}

</span><span>/*</span><span>匹配一张图片
$pattern = '/"pics"(.+?)","position"/is';
preg_match_all($pattern, $texts, $matchpic);
for($i=0;$i<count($matchpic[0]);$i++){
    $pattern3 = '/img.alicdn(.+?).jpg/is';
    preg_match($pattern3, $matchpic[0][$i], $matchpic_only);
    echo "<p>".$matchpic_only[0]."</p>";
}</span><span>*/</span>

<span>/*</span><span>匹配所有图片
$pattern = '/"pics"(.+?)","position"/is';
preg_match_all($pattern, $texts, $matchpic);
for($i=0;$i<count($matchpic[0]);$i++){
    $pics_str=str_replace('"pics":["//','',str_replace('"],"picsSmall":"","position"','',$matchpic[0][$i]));
    $arr = explode('","//',$pics_str);
    echo "<p>";
    foreach($arr as $newstr){
        echo '<img src=http://'.$newstr.' width=100 >';
    }
    echo "</p>";
}</span><span>*/</span>
?>
Copy after login

Is there any good way to parse jsonp format? Please God~~~

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1119217.htmlTechArticleExample of using PHP to capture user’s order and comment pictures of Taobao products. Why do you want to do this function? It’s because when I was working on a Taoke website some time ago, I thought about whether I could capture Taoke...
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