I'm trying to use Pubsubhubub to get real time RSS feeds update. I'm using PHP for that purpose.
I subscribed to thenextweb as an example;
$hub_url = "http://pubsubhubbub.appspot.com/";
$callback_url = "http://xx.com/rss/callback.php";
$feed = "http://feeds2.feedburner.com/thenextweb";
$sub = new Subscriber($hub_url, $callback_url);
$status = $sub->subscribe($feed);
I receive The hub returns code 202, and after that a "GET" response to my callback.php with the hub_challenge and other stuff. I followed what the tutorials suggest of echoing this number, and hence, the hub will be able to push updates to my callback.
if ($method == 'GET' && $_GET['hub_mode'] == 'subscribe') {
$challenge = $_GET['hub_challenge'];
header('HTTP/1.1 200 "OK"', null, 200);
header('Content-Type: text/plain');
echo $challenge;
}
That's how I echo the challenge number. The problem here is that I don't get any other messages from the hub even though i have a condition to handle any POST message in my callback.
else if ($method == 'POST') {
$updates = json_decode(file_get_contents("php://input"), true);
//doing stuff with the data here
}
I'm not sure if the problem is with the echo part or after that. Does anyone have similar issues? what am I doing wrong?
I just solved the problem. Apparently I was using a different topic_url, I was using this link: http://feeds.feedburner.com/TheBoyGeniusReport?format=xml. Instead, view the page source and make sure you are using the link inside href. The highlighted link below is what you're supposed to use.
... xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/TheBoyGeniusReport"...
Related
I am trying to validate if the input of a url, actually exists or not. I have been trying the following code, however I got no success. This is the following code:
Using cURL:
<?php
$url = 'https://github.com';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 404 (file not found). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 404) {
echo "Url not working";
}
echo "true";
?>
Using get_headers
<?php
// Initialize an URL to the variable
$url = "https://www.geeksforgeeks.org";
// Use get_headers() function
$headers = #get_headers($url);
// Use condition to check the existence of URL
if($headers && strpos( $headers[0], '200')) {
$status = "URL Exist";
}
else {
$status = "URL Doesn't Exist";
}
// Display result
echo($status);
?>
I have searche both these answers on stackoverflow and other websites, and used these to check if the url I give actually exists. When I write down a non existing url, I would like it to output that the website does not exists, however, I always end up having the same output as an existing url, meaning that somethig might be wrong, although I cannot fully see it. By the first code, the output is always true, even if the url does not exist. By the second code, the output is always 'URL doesn't exist', even if the url actually exists. Am I doing something wrong? I am using PHP Version 7.4, is this tool still working? I apologize if the question is not clear.
I'm working on an integration between Slack and Filemaker utilizing PHP. I am successful in having the code create a record in Filemaker based on the json request, and also have no trouble returning the challenge key to Slack.
However, I'm having trouble passing the header response 200 OK to Slack, while passing the challenge back. It looks like it has to be one or the other.
I've tried to move the HTTP header to different areas in the code, but haven't had any success so far.
Here is the current code:
<?php
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data["challenge"])) {
$body = $_SERVER['HTTP_X_SLACK_RETRY_REASON'];
require_once ('Filemaker.php');
//$body = file_get_contents('php://input');
$fm = new Filemaker();
$fm->setProperty('database', '');
$fm->setProperty('username', '');
$fm->setProperty('password', '');
$command = $fm->newPerformScriptCommand('PHP_RESPONSE', 'script', $body);
$result = $command->execute();
}
else {
header("Content-Type: text/plain");
header('X-PHP-Response-Code: 200', true, 200);
echo $data["challenge"];
}
?>
The result I expect is for the code to return the challenge code for Slack, while also returning an HTTP header of 200 OK.
Currently I can see I am receiving an error of "http_error" from Slack, which is what leads me to believe the problem is that the header is not being passed back successfully.
Any ideas on what is wrong, or suggestions on the right direction to proceed would be greatly appreciated.
The problem was occurring because for events slack doesn't send "challenge" as a parameter when sending events. It looks like echoing "challenge" is only needed when initially setting the URL for the events API.
I enclosed the challenge echo in a if statement that would only trigger if the challenge variable was present. After doing so the 200 OK was successfully passed.
Here is the code I used that solved the problem for me:
$data = json_decode(file_get_contents('php://input'), true);
if (isset($data["challenge"])) {
$message = [
"challenge" => $data["challenge"]
];
header('Content-Type: application/json');
echo json_encode($message);
}
The documentation is actually a bit inconsistent on this topic. It claims you can respond the challenge in plan text, but the example shows it as x-www-form-urlencoded.
To be on the safe side try returning the challenge as JSON. That works perfectly for me. You also do not need to explicitly set the HTTP 200 code.
Example code:
$message = [
"challenge" => $data["challenge"]
];
header('content-type: application/json');
echo json_encode($message);
I own a website that generates YouTube names, how can I show the user if a channel is already taken or not?
You can use channel->list request for this.
As mentioned in documents, all you need to do is do
channels.list(part="id", forUsername="username")
If this return an empty list, there is no channel with that username.
Also here are some samples to get you started.
You can try
$api = 'https://gdata.youtube.com/feeds/api/users/';
$user = "user";
$headers = get_headers($api . $user, true);
if ($headers[0] == "HTTP/1.0 200 OK") {
// its ok
}
I'm setting up a site where users will be able to post links, and curl (in php) will crawl the url, and format something based on the metadata, open graph tags, etc. I have it set up to run simultaneous uploads with multi_init and multi_exec. I created a gist for the class here. What it's supposed to do is:
get metadata from multiple urls
return a single json string but only for pages with content-type 'text/html' (so don't bother with direct links to images, js, executables, etc)
The problem seems to be the callback for CURLOPT_HEADERFUNCTION. I thought that having it return -1 when a content-type header exists but isn't an html header would abort the download but it doesn't seem to do anything (although the check appears correct and it seems to be returning -1.) It still seems to allow any content type through.
Here specifically is the callback:
CURLOPT_HEADERFUNCTION => function($ch, $header){
// if they're sending a content-type header, it must be text/html
if(stripos(trim($header), "Content-Type") === 0){
list($key, $val) = explode(":", $header);
if(stripos(trim($val), "text/html") === 0){
return strlen($header);
}
else{
return -1;
}
}
else{
return strlen($header);
}
}
I tried curl_close but got an error about closing curl in a callback. Any suggestions?
Use the callback to set a (global) variable. Skip your curl_exec() call when false.
$htmlheader = true;
function header_callback($ch, $headers)
{
$GLOBALS['htmlheader']=false;
}
$ch = curl_init('http://www.example.com/');
curl_setopt($ch,CURLOPT_HEADERFUNCTION, 'header_callback');
if($htmlheader)
{
$result = curl_exec($ch);
}
curl_close($ch);
I am currently trying to fetch some facebook data, which I then want to access in Javascript. Specifically, I am trying to access some characteristics of the user's friends.
So I am getting the user's friend list using file_get_contents to his graph API URL.
This provides me with an array of friend ids.
As I need a characteristic from each friend, I am doing:
foreach($dataarray as $friend) {
$friendurl = "https://graph.facebook.com/".$friend->id."?access_token=".$token."";
$fdata = json_decode(file_get_contents($friendurl));
if($fdata->gender == "male") {
array_push($fulldata, $fdata->name);
}
}
Having this code piece seems to break the javascript code, as none of my alert instructions are ran.
Also, inserting a break after the if, so that only one file_get_contents is done, seems to make the code runnable (but I obviously need to go through all of the friends).
How can I solve this?
I would use jQuery or xmlHttpRequest to do the HTTP GET, but somehow I always seem to get back a status code of 0, with an empty response.
Edit:
Here is the JS code:
<script type="text/javascript">
function initialize() {
alert('Test1');
<?php
$fulldata = array();
$data = $result->data;
foreach($data as $friend) {
$friendurl = "https://graph.facebook.com/".$friend->id."?access_token=".$token."";
//echo("alert(\"".$friendurl."\");");
$fdata = json_decode(file_get_contents($friendurl));
if($fdata->hometown->name) {
array_push($fulldata, $fdata->hometown->name);
}
}
echo ("alert(\"".count($fulldata)."\")");
?>
}
</script>
I should've also added that this is being done on a page embedded into facebook using the canvas feature.
Try...
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($ch);
curl_close ($ch);
}
foreach($dataarray as $friend){
$friendurl = "https://graph.facebook.com/".$friend->id."?access_token=".$token."";
$fdata = json_decode(curl($friendurl));
if($fdata->gender == "male"){
array_push($fulldata, $fdata->name);
}
}
Maybe FGC is disabled but you don't get any notifications/warnings.
Code from comment:
error_reporting(E_ALL); ini_set("display_errors", 1);
Note that you are doing cross-domain AJAX call which is prohibited for security reasons.
You can do the api call on the server and echo the data to the client side JS, or you can build a php proxy return the result of the Graph API call(As the proxy is at your own server, they are in the same domain).