So I almost have a pingback sender ready for my rails app (people post links to content and donate to them). Almost.
I've borrowed heavily from the code here: http://theadmin.org/articles/2007/12/04/mephisto-trackback-library/
I modified the slightly for my purposes:
require 'net/http'
require 'uri'
class Trackback
@data = { }
def initialize(link_id)
link = Link.find(link_id)
site = Link.website
if link.nil?
raise "Could not find link"
end
if link.created_at.nil?
raise "link not published"
end
@data = {
:title => link.name,
:excerpt => link.description,
:url => "http:://www.MyApp.org/links/#{link.to_param}/donations/new",
:blog_name => "My App"
}
end
def send(trackback_url)
u = URI.parse trackback_url
res = Net::HTTP.start(u.host, u.port) do |http|
http.post(u.request_uri, url_encode(@data), { 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8' })
end
RAILS_DEFAULT_LOGGER.info "TRACKBACK: #{trackback_url} returned a response of #{res.code} (#{res.body})"
return res
end
private
def url_encode(data)
return data.map {|k,v| "#{k}=#{v}"}.join('&')
end
end
Looks like I'm sending links successfully to my wordpress开发者_如何学运维 blog but when I look at the link displayed on the trackback I get this: http://www.theurl.com/that/my/browser/iscurrentlypointing/at/http:://www.MyApp.org/links/#{link.to_param}/donations/new"
All I want is the second half of this long string. Don't know why the current location on my browser is sneaking in there.
I've tried this on two of my blogs so it doesn't seem to be problem related to my wordpress installation.
UPDATE: Okay this is a little odd: I checked the page source and it shows the correct link. When I click on it, however, I get directed to the weird link I mentioned above. Is this a Wordpress Issue?
Whoops! Looks like it was just a syntax error. A sneaky double colon
This line
url => "http:://www.MyApp.org/links/#{link.to_param}/donations/new"
Should of course be like this
url => "http://www.MyApp.org/links/#{link.to_param}/donations/new",
精彩评论