百度資源平臺API提交推送示例分為四種,分別是curl推送示例、post推送示例、php推送示例、ruby推送示例。
1、curl推送示例
將要提交的鏈接按照每行一條的格式寫入一個文本文件中,命名此文件為urls.txt,然后進入該文件所在目錄,執(zhí)行如下命令:
curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?appid=1584274397481205&token=lZv5hfmigdZjU7Ws&type=realtime"
使用php、python、java等可以參照這個過程推送結構化數(shù)據(jù)。
2、post推送示例
POST /urls?appid=1584274397481205&token=lZv5hfmigdZjU7Ws&type=realtime?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
3、php推送示例
$urls = array(
? ?'http://www.example.com/1.html',
? ?'http://www.example.com/2.html',
);
$api = 'http://data.zz.baidu.com/urls?appid=1584274397481205&token=lZv5hfmigdZjU7Ws&type=realtime'
$ch = curl_init();
$options = ?array(
? ?CURLOPT_URL => $api,
? ?CURLOPT_POST => true,
? ?CURLOPT_RETURNTRANSFER => true,
? ?CURLOPT_POSTFIELDS => implode("
", $urls),
? ?CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
echo $result;
4、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?appid=1584274397481205&token=lZv5hfmigdZjU7Ws&type=realtime')
req = Net::HTTP::Post.new(uri.request_uri)
req.body = urls.join(" ")
req.content_type = 'text/plain'
res = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(req) }
puts res.body
閱讀本文的人還可以閱讀: