2

I'm trying to replace RSS polling with PubSubHubbub on my site. I'm able to use the subscriber library that google offers to send the subscription request. From the code it looks like it sends a post request via cURL with the RSS URL and a callback URL.

So this is where I need some direction:

In order to complete the subscription request my callback URL has to receive a GET request and then echo back a value from the GET request along with a 200 response. How do I get the parameters from the GET request? Is the echo done again via cURL? If so what option should include the 200 response?

cweiske
  • 30,033
  • 14
  • 133
  • 194

2 Answers2

4

This very simple script should be a start:

  echo $_GET["request_name"];

this will output the GET parameter request_name and (implicitly) send a 200.

It's also a good idea to explicitly declare a content type before echoing, to prevent the default content type (usually "text/html") from kicking in:

header("Content-type: text/plain");

Note that when echoing external data, you may need to sanitize the output first - if the for example the output format is HTML, you would want to do something like echo htmlspecialchars($_GET["request_name"]); to prevent Cross-Site Scripting.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • That was the first thing I tried. The GET request is suppose to include a parameter called "hub.challenge". So I used $_GET and the tried writing the value to a txt file. I added a string as well, and it does reach the callback as it shows the string in the txt file but not the value from the parameter using $_GET. – Gerardo Calixto Aquino Jan 26 '10 at 19:25
  • $request = $_GET['hub.challenge']; fh = fopen('test.txt, 'a') or die("can't open file"); fwrite($fh, 'Test: '.$request); fclose($fh); – Gerardo Calixto Aquino Jan 26 '10 at 19:27
  • Hmm. That looks o.k. and should definitely output a GET parameter of that name. The dot in the variable name shouldn't be a problem either. Can you confirm first that your script is acutally being called with the parameter, for example by checking the access logs? What is your PHP version? – Pekka Jan 26 '10 at 19:29
  • @transient-jet-lag: did you copy and paste that code? you're missing a `$` somewhere ... :) – munch Jan 26 '10 at 19:34
  • @transient-jet-lag - what do you see with print_r(); ? – Jay Zeng Jan 26 '10 at 19:36
  • Version is 5.2.6, I think this is it in the log: [26/Jan/2010:00:27:44 -0800] "GET /xxx.com/callback.php?hub.topic=feedurl&hub.verify_token=&hub.challenge=0fa1411d855167a519c39073dd8c7725&hub.lease_seconds=&hub.mode=subscribe HTTP/1.1" 200 - "-" "Superfeedr Hubbub : http://superfeedr.com/hubbub" 70.32.68.118 It's right there too.. hub.challenge=0fa1411d855167a519c39073dd8c7725 – Gerardo Calixto Aquino Jan 26 '10 at 19:38
  • That looks all right. Can you follow @Jay Zeng's advice? You can write the result of the print_r operation into a file: `fwrite($fh, print_r($_GET, true));` – Pekka Jan 26 '10 at 19:43
  • Well now that's just sad, documentation and log specifies it as 'hub.challenge' and the print_r shows that it's hub_challenge.. Thanks everyone. Why is it almost always the most simple things?!? – Gerardo Calixto Aquino Jan 26 '10 at 19:47
  • Strange. Could that be PHP turning the `.` into a `_` to make it accessible as a variable (dots are forbidden in PHP variable names) It would be the first time that I hear this, though. On the other hand, the docs could lie but the logs won't. – Pekka Jan 26 '10 at 19:49
  • Yeah, I don't know what's going on with that either.. But at least now I can concentrate on echoing the request.. I think I know how, so I'll give you the credit. – Gerardo Calixto Aquino Jan 26 '10 at 19:53
  • 4
    Note: Dots and spaces in variable names are converted to underscores. For example becomes $_REQUEST["a_b"]. From PHP doc. – Gerardo Calixto Aquino Jan 26 '10 at 19:56
  • " becomes $_REQUEST["a_b"]" - weird, don't know this before. Thanks for sharing. – Jay Zeng Jan 26 '10 at 20:43
  • @transient-jet-lag - by the way, what version of PHP are you running? – Jay Zeng Jan 26 '10 at 20:45
  • I wouldn't advise directly echoing out any `$_GET` variables as such. Even if it seems benign these sorts of answers and copy / pasted without further reading too often, you should always make sure the variable is what you expect and escape / sanitize where needed! – Benji Mar 19 '18 at 13:51
  • 1
    @Benji the expected response content type here looks like it wouldn't be HTML but something like `text/plain`, which would take care of those problems but you're right, I'll add a note about sanitizing outputs and explicitly declaring the content type. – Pekka Mar 19 '18 at 14:19
2

There was recently a thread on the php-dev mailing list about this. The reason you can't access 'hub.challenge' in the $_GET superglobal is due to register_globals. Basically PHP cleans up any argument names before creating the superglobals. Any dots will be converted to underscores. It's looking to be 'fixed' in PHP 6, but not before due to BC issues.

Here's the thread about it.