import urllib
import urllib2
url = 'http://www.sentiment140.com/api/bulkClassifyJson'
values = {'data': [{'text': 'I love Titanic.'}, {'text': 'I hate Titanic.'}]}
data = urllib.urlencode(values)
response = urllib2.urlopen(url, data)
page = response.read()
我如何在PHP中写这个?我知道你的urlencode for php但是有没有一个函数让我通过传递url和编码值如上面的python代码打开一个url?
答案 0 :(得分:2)
好,你可以使用这样的东西,如果:
$url = 'http://www.sentiment140.com/api/bulkClassifyJson?';
$data = array (
'data' => urlencode('I love Titanic.'),
'text' => urlencode('I hate Titanic.')
);
foreach ($data as $key => $value) {
$url .= $key . '=' . $value . "&";
}
$curl = curl_init();
$timeout = 5;
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
$result = curl_exec($curl);
curl_close($curl);
echo $result;
你也可以使用POST方法和 n 其他东西..
我建议您尝试查看有关CURL的更多信息:http://br.php.net/manual/en/book.curl.php
答案 1 :(得分:1)
<?php
$url = 'http://www.sentiment140.com/api/bulkClassifyJson';
$values = {'data': [{'text': 'I love Titanic.'}, {'text': 'I hate Titanic.'}]};
$response = http_post_fields(urlencode($url),json_decode($values,true));
?>