這是一個百度站長平台的推送案例:
post案例:
POST /urls?site=www.nantongzt.com&token=xxxxxx HTTP/1.1
User-Agent: curl/7.12.1
Host: data.zz.baidu.com
Content-Type: text/plain
Content-Length: 83
http://www.example.com/1.html
http://www.example.com/2.html
php的推送案例:
$urls = array( 'http://www.example.com/1.html', 'http://www.example.com/2.html',);$api = 'http://data.zz.baidu.com/urls?site=www.nantongzt.com&token=xxxxxx';$ch = curl_init();$options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode("\n", $urls), CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),);curl_setopt_array($ch, $options);$result = curl_exec($ch);echo $result;
ruby的案例
require 'net/http'urls = ['http://www.example.com/1.html', 'http://www.example.com/2.html']uri = URI.parse('http://data.zz.baidu.com/urls?site=www.nantongzt.com&token=xxxxxx')req = Net::HTTP::Post.new(uri.request_uri)req.body = urls.join("\n")req.content_type = 'text/plain'res = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }puts res.body
其實應該很簡單,就是如何用python post提交資料.
但是看php和ruby的代碼又不是很像~
希望有高人指點一下,個人比較菜~希望有翻譯得詳細一點,謝謝了!
回複內容:
這是一個百度站長平台的推送案例:
post案例:
POST /urls?site=www.nantongzt.com&token=xxxxxx HTTP/1.1
User-Agent: curl/7.12.1
Host: data.zz.baidu.com
Content-Type: text/plain
Content-Length: 83
http://www.example.com/1.html
http://www.example.com/2.html
php的推送案例:
$urls = array( 'http://www.example.com/1.html', 'http://www.example.com/2.html',);$api = 'http://data.zz.baidu.com/urls?site=www.nantongzt.com&token=xxxxxx';$ch = curl_init();$options = array( CURLOPT_URL => $api, CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_POSTFIELDS => implode("\n", $urls), CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),);curl_setopt_array($ch, $options);$result = curl_exec($ch);echo $result;
ruby的案例
require 'net/http'urls = ['http://www.example.com/1.html', 'http://www.example.com/2.html']uri = URI.parse('http://data.zz.baidu.com/urls?site=www.nantongzt.com&token=xxxxxx')req = Net::HTTP::Post.new(uri.request_uri)req.body = urls.join("\n")req.content_type = 'text/plain'res = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }puts res.body
其實應該很簡單,就是如何用python post提交資料.
但是看php和ruby的代碼又不是很像~
希望有高人指點一下,個人比較菜~希望有翻譯得詳細一點,謝謝了!
#coding=utf-8import requestsurls = [ 'http://www.example.com/1.html', 'http://www.example.com/2.html']api = 'http://data.zz.baidu.com/urls?site=www.nantongzt.com&token=xxxxxx'data = '\n'.join(urls)headers = {'Content-Type': 'text/plain'}r = requests.post(api, data=data, headers=headers)print r.text