| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 | <?php
/**
 *  
 *作者:@Belin_love  
 *来源:http://52its.sinaapp.com/  
 *日期:2012.11.27  
 *  
 **/
//function simsimi($keyword)   
//{   
//做成API接口的话,请发起GET请求,返回Josn   
//只自己用的话,封装成一个函数,返回JSON字段中的response   
if(isset($_GET['key'])){
    $keyword = $_GET['key'];
    $url = "http://www.simsimi.com/talk.htm?lc=ch";
    //这个curl是因为官方每次请求都有唯一的COOKIE,我们必须先把COOKIE拿出来,不然会一直返回“HI”   
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $content = curl_exec($ch);
    curl_close($ch);
    list($header, $body) = explode("\r\n\r\n", $content);
    preg_match("/set\-cookie:([^\r\n]*)/i", $header, $matches);
    //curl_setopt($ch, CURLOPT_COOKIE, $cookie);   
    $cookie = $matches[1];
    $urll = 'http://www.simsimi.com/func/req?msg=' .$keyword. '&lc=ch';
    // 这个CURL就是模拟发起请求咯,直接返回的就是JSON   
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $urll);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_REFERER, "http://www.simsimi.com/talk.htm?lc=ch");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    $content = curl_exec($ch);
    curl_close($ch);
    //输出json   
    print_r($content);
    /*$reply = '你说的话太高深啦,我还听不懂,你可以教教我吗?[兔子]';   
    $json = json_decode($json, 1);  
    if (isset($json['response'])) {  
        $reply = $json['response'];  
    }  
    echo $reply;  
}*/
}
?>  
 |