XML-RPC pinging (google and others) - php

I am trying to ping (SEO tactic called "ping" is used for new content to get robots index it faster) Google in PHP. Only thing I know is that I need to send my request to following url:
http://blogsearch.google.com/ping/RPC2
Probably I can use PHP XML-RPC functions. I don't know how to format my request and which method to use.

As far as you're concerned to do the XML-RPC request (Example #1).
If you follow the specification of a pingback, it would look like this:
$sourceURI = 'http://example.com/';
$targetURI = 'http://example.com/';
$service = 'http://blogsearch.google.com/ping/RPC2';
$request = xmlrpc_encode_request("pingback.ping", array($sourceURI, $targetURI));
$context = stream_context_create(array('http' => array(
'method' => "POST",
'header' => "Content-Type: text/xml",
'content' => $request
)));
$file = file_get_contents($service, false, $context);
$response = xmlrpc_decode($file);
if ($response && xmlrpc_is_fault($response)) {
trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
} else {
print_r($response);
}
Which would give you the following output:
Array
(
[flerror] =>
[message] => Thanks for the ping.
)
Generally, if you don't know which method you call, you can also try XML-RPC Introspection - but not all XML-RPC servers offer that.
You asked in a comment:
According to specs, $targetURI should be: "The target of the link on the source site. This SHOULD be a pingback-enabled page". How can I make pingback enabled page, or more important, what is that actually?
A pingback enabled site is a website that announces an XML-RPC pinbback service as well. That's done with the HTMl <link> element in the <head> section. Example:
<link rel="pingback" href="http://hakre.wordpress.com/xmlrpc.php" />
The href points to an XML-RPC endpoint that has the pingback.ping method available.
Or it's done by sending a specifc HTTP response header:
X-Pingback: http://charlie.example.com/pingback/xmlrpc
See pingback-enabled resource.
So if you ping others, others should be able to ping you, too.

Ummm, I think we should use weblogUpdates.ping or weblogUpdates.extendedPing instead of pingback.ping to ping a site about new content.
pingback.ping is for a new link from one site to another, not for new content.

I think you should use weblogUpdates.extendedPing with google blog search, weblogs, Pingomatic and weblogUpdates.ping for other server. I created a ping tool but some server return an error with http and https, i cannot give a bug Online rpc xml ping

Related

PHP simple_html_dom load_file with user agent

I need to crawl a web site with simple_dom_html->load_file(),and i need include a user agent,follow is my code,but i don't know if my code is right or there has a good way to achieve my needs.thanks in advance
$option = array(
'http' => array(
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
)
);
$context = stream_context_create($option);
$simple_html_dom = new simple_html_dom();
$simple_html_dom -> load_file(CRAWLER_URL, false, $context);
I have tested your method / code and I can confirm it works as intended: the user-agent in the HTTP header send, is correctly changed to the one you provide with the code. :-)
As for your uncertainty: I usually use the curl functions to obtain the HTML string (http://php.net/manual/en/ref.curl.php). In this way I have more control of the HTTP request and then (when anything works fine) I use the simple_dom_html→str_get_html() function on the HTML string I get with curl. So I am more flexible in error handling, dealing with redirects and I had implemented some caching...
The solution for your problem was simply to grep a URL like http://www.whatsmyuseragent.com/ and lock in the result for the user-agent string used in the request, to check if it had worked as intended...

Read an Amazon Cloudfront API Result in PHP

I've got a PHP script that submits Invalidation requests to Amazon's Cloudfront via API's.
I can capture the response, but it comes back as text basically looking like this:
HTTP/1.0 201 Created
Content-Type: text/xml
Location: https://cloudfront.amazonaws.com/2012-07-01/distribution/distribution ID/invalidation/invalidation ID
<Invalidation xmlns="http://cloudfront.amazonaws.com/doc/2012-07-01/">
<Id>IDFDVBD632BHDS5</Id>
<Status>InProgress</Status>
<CreateTime>2013-04-16T19:37:58Z</CreateTime>
<InvalidationBatch>
<Paths>
<Items>
<Path>/image1.jpg</Path>
</Items>
</Paths>
<CallerReference>20130416090001</CallerReference>
</InvalidationBatch>
</Invalidation>
I basically just want to grab the status value, and I suppose I could do it via Regex or some string manipulation, but I'm assuming there is a better way to convert the returned data into an object and access it properly.
I tried:
$dom = new DOMDocument();
$dom->loadXML($data);
But $data doesn't work because it literally contains the header portion "HTTP/1.0 201..."
Anyone know the proper way to handle this?
What script are you using? Have you considered using the AWS SDK for PHP (https://github.com/aws/aws-sdk-php)?
Using the AWS SDK for PHP you could do the following:
// Instantiate a CloudFront client
$cloudfront = \Aws\CloudFront\CloudFrontClient::factory(array(
'key' => 'your-aws-access-key-id',
'secret' => 'your-aws-secret-key',
));
// Get the status of an invalidation
$invalidationStatus = $cloudfront->getInvalidation(array(
'DistributionId' => 'your-distribution-id',
'Id' => 'your-invalidation-id',
))->get('Status');
Usually the Http client library should do that, you didn't mention the used one.
Still parsing that response should be simple as:
$dom->loadXML(substr($data, strpos("\n\n", $data)+2))
Got this figured out by doing the following.
$resp = substr($resp, strpos($resp, "\r\n\r\n")+4); // This strips out the header
$outputxml = simplexml_load_string($resp); // Converts it to an object in PHP which can be fed back more easily

How to send URL variables programmatically with response from my PHP endpoint

I have a basic API endpoint set up on my site, which a 3rd party site will use to verify certain info that is entered into a form by the user.
Here's the flow:
1. User is on 3rd party site.
2. User enters info into a form
3. Info is sent to my site's endpoint.
4. My site checks the information and returns a JSON object.
As you can see from #4, my API is currently set up to return a JSON object. After the info is checked, something like this happens:
header('content-type: application/json; charset=utf-8');
echo json_encode($response);
exit;
However, the 3rd party site is only set up to receive URL variables. Is there a way to pass back url variables programmatically? I realize I could theoretically send a new request, but it's not clear to me where that request should go (the internal workings of the 3rd party site aren't well documented), so I'd much prefer to send it as a response.
I hope this makes sense. Please comment if it doesn't. Thanks in advance!
You don't get to send GET/POST parameters in the response, but in the response body you can send whatever you want in whatever format you want - and they can use curl or file_get_content and parse it on their side (3rd party's website).
For example (on the 3rd party's website):
//setting a call to your server
$opts = array('http' =>
array(
'method' => 'POST',
'header' => "Content-Type: text/xml\r\n".
"Authorization: Basic ".base64_encode("$https_user:$https_password")."\r\n",
'content' => $body,
'timeout' => 60
)
);
$context = stream_context_create($opts);
$url = 'https://'.$https_server;
// Here they call your server
$result = file_get_contents($url, false, $context, -1, 40000);
// Here you'll parse the $result

How many ways does PHP have to open a URL with POST data?

I'm writing a PHP error collection widget that collects errors from an application and then by making a POST request to a URL, reports them.
How many different ways to do this are there, and how would you detect whether they are available? On different servers extensions may not be installed and security options may be turned off so I'd like to try as many as possible.
CURL is one; what's the best way to check whether this is installed?
fopen() can do URL's if it has permission but I don't think you can send POST data?
http://uk3.php.net/manual/en/book.http.php looks like it needs curl, so we may as well just use curl.
PHP sockets could be used, and just write the HTTP protocol yourself; any good libraries for this?
This service is open source under the BSD license BTW: http://elastik.sourceforge.net/
The Zend_Http_Client implements four different ways of connecting to a target and can be easily extended (of course you don't need to use the complete framework for it ;).
To answer at least part of your question, you should be able to detect whether curl is loaded with extension_loaded():
if (!extension_loaded('curl')) {
# Yay curl!
}
Oh wait, It looks like fopen can include POST data: http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl
EDIT: couldn't get code from that URL to work without a slight edit, so here is my final code:
if (ini_get('allow_url_fopen')) {
$params = array('http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header'=> "Content-type: application/x-www-form-urlencoded\r\n",
));
$ctx = stream_context_create($params);
$fp = fopen($url, 'rb', false, $ctx);
if (!$fp) {
print "FOPEN ERROR!";
die();
}
fclose($fp);
}
Snoopy is popular and fully functional library for making HTTP requests.

PHP Get Content of HTTP 400 Response

I am using PHP with the Amazon Payments web service. I'm having problems with some of my requests. Amazon is returning an error as it should, however the way it goes about it is giving me problems.
Amazon returns XML data with a message about the error, but it also throws an HTTP 400 (or even 404 sometimes). This makes file_get_contents() throw an error right away and I have no way to get the content. I've tried using cURL also, but never got it to give me back a response.
I really need a way to get the XML returned regardless of HTTP status code. It has an important "message" element that gives me clues as to why my billing requests are failing.
Does anyone have a cURL example or otherwise that will allow me to do this? All my requests currently use file_get_contents() but I am not opposed to changing them. Everyone else seems to think cURL is the "right" way.
You have to define custom stream context (3rd argument of function file_get_contents) with ignore_errors option on.
As a follow-up to DoubleThink's post, here is a working example:
$url = 'http://whatever.com';
//Set stream options
$opts = array(
'http' => array('ignore_errors' => true)
);
//Create the stream context
$context = stream_context_create($opts);
//Open the file using the defined context
$file = file_get_contents($url, false, $context);

Categories