Skip to content

Instantly share code, notes, and snippets.

@jeremycherfas
Last active September 5, 2017 15:52
Show Gist options
  • Save jeremycherfas/cf450042b2a0a2660096fd6187b9a3d6 to your computer and use it in GitHub Desktop.
Save jeremycherfas/cf450042b2a0a2660096fd6187b9a3d6 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>PHP tests</title>
<meta name="generator" content="BBEdit 11.6" />
</head>
<body>
<h1>This is a heading</h1>
<?php
$feedUrl = 'https://www.reading.am/jeremycherfas/posts.rss';
$cacheFile = 'RSSCache.txt';
// Set unchanging Known variable
$known['action'] = "/like/edit";
$known['username'] = "USERNAME";
$known['known_api_key'] = "API";
$known['token'] = base64_encode(hash_hmac('sha256',$known['action'] ,$known['known_api_key'] , true));
// retrieve $old_guid from cachefile
$old_guid = file_get_contents($cacheFile);
echo "<h1>$old_guid</h1>";
$myXMLData = file_get_contents('xmlstore.txt');
$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object"); //Creates JSON?
$json = json_encode($xml); // not an ARRAY
$results = json_decode($json, TRUE); //an array
$results = $results['channel']; // discards first array
$results = $results['item']; // discards description of feed
$latest_item = array_shift($results); // pops first array element off
$latest_guid = $latest_item['guid']; // gets value for $latest_guid
array_unshift($results, $latest_item); // prepends $latest_item Back to $results
// Where in $results is $old_guid and slice
$item_count = 0;
Foreach ($results as $result) {
$count ++;
If (in_array($old_guid, $result)){
$item_count = ($count-1);
}
}
array_splice($results, $item_count); // Leaves only the new items in $results
Foreach ($results as $result){
//Get rid of the incoming stuff with something like
$mylink = preg_replace('@^.+?/p/[^/]+/@', '', $result['link']); // Clean link
$mydescription = 'PESOS from <a href="' . $result['guid'] .'"'. '>reading.am</a>'; // Link to Reading.am
$mybody = [$mylink, $mydescription]; // array to send
echo '<h3>', var_dump($mybody), '</h3>';
echo '<h3>', json_encode($mybody), '</h3>';
$known['body'] = json_encode($mybody);
// $known['body'] = ($mybody);
$known['headers'] = array('Accept: application/json',
'X-KNOWN-USERNAME: ' . $known['username'],
'X-KNOWN-SIGNATURE: ' .$known['token'],
'Content-Type: application/json',
'Content-Length: ' . strlen($known['body']));
Echo "<br /><br />";
Echo '<pre>', var_dump($known['body']), '</pre>';
// $ch = curl_init();
// // curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookiefile");
// curl_setopt($ch, CURLOPT_URL, 'https://stream.jeremycherfas.net' . $known['action']);
// curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $known['body']);
// curl_setopt($ch, CURLOPT_HTTPHEADER, $known['headers']);
// curl_exec ($ch);
// curl_close ($ch);
}
// retrieve $old_guid from cachefile
$old_guid = file_get_contents($cacheFile);
echo "<h1>new . $old_guid . </h1>";
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment