I am using phpfickr and need to run it in CLI.
But when executing $ php getToken.php, but I'm not being able to getting authenticated. I have the $app_id and $secret.
Please, I am new to this and haven't found a correct solution.
The phpFlickr library you've linked to is very old.
Its most recent commit was on July 7, 2014, roughly 2.5 years ago.
It uses PHP 4 style constructors (named after the class), which have been deprecated in PHP 7 and will be removed in a future version of PHP.
It uses var keywords, another PHP 4 anachronism, which were briefly deprecated but then brought back as a semi-synonym of public.
If you really want to use this library it should be as simple as
<?php
require_once __DIR__ . '/phpflickr/phpFlickr.php';
// Make sure to fill in your API key and secret!
$flickr = new phpFlickr('your-api-key-goes-here', 'your-api-secret-goes-here');
The getToken.php file that you referenced does this. Perhaps you forgot to fill in your API key and secret?
Once you have your $flickr object you can use it to interact with Flickr's API. For example, you can do something like this to see titles of recently-posted public photos:
foreach ($flickr->photos_getRecent()['photos']['photo'] as $photo) {
echo $photo['title'] . "\n";
}
However, there are more modern options. rezzza/flickr, for example, is available on Packagist and has over 16K installs. It uses modern PHP features like namespaces, __construct() constructors, and visibility keywords. It also seems to have a more sane API, though that's subject to opinion.
If you are already using Composer you should be able to composer require rezzza/flickr, then proceed as its README suggests. If you're not using Composer, start. It is an important part of the modern PHP ecosystem.
Based on issue https://github.com/dan-coulter/phpflickr/issues/48 , adding
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
got me going.
This issue appears to be merged into master branch, but it does not exist.
Related
I'm coming back to PHP after many years and am running into a bit of an odd problem. I'm running PHP 7.1.7 on Windows and am trying to install and utilize the maclof/kubernetes-client library via Composer. I have the following simple code snippet to test things out:
<?php
require __DIR__ . '/vendor/autoload.php';
use Maclof\Kubernetes\Client;
$path = realpath(__DIR__ . '/kubeconfig.yaml');
// this works (when fully qualified)
$config = Maclof\Kubernetes\Client::parseKubeconfigFile($path);
// but this doesn't
$client = new Client($config);
?>
As you can see, when I access the static method parseKubeconfigFile using the fully-qualified class name (Maclof\Kubernetes\Client), this works. But when I try to just use the shorter name of Client, it doesn't work. Even class_exists("Client") returns false. It's as though PHP is outright ignoring the use statement, and I don't know why.
What am I missing? Is there some weird php.ini directive that governs use statements? Or is there some wacky bug in this specific version of PHP? I'm pulling my hair out right now trying to understand why this doesn't work.
So I'm basically trying to create a tool that used across platforms, including sometimes legacy php version.
I don't plan on supporting anything less than 5.4 so I'd like to use something like below; however, instead of the application dying, I get various syntax errors. One of the first to start alerting is using brackets to define arrays.
Is there anyway to get around this?
if (version_compare(phpversion(), '5.4', '<')) {
die('This tool does not support anything < PHP 5.4<br>Your PHP version is: '.phpversion() );
}
$array= ['a','b','c'];
The file you are doing a version_check for should simply not use any newer PHP features or include any files that do. If you want the version_check to work on PHP 4, it can only use PHP 4 features.
When I try to send an attachment using the SendGrid PHP library (https://github.com/sendgrid/sendgrid-php), the function fails (white screen). Removing the "setAttachment" line makes it work again.
Here is my code:
require "sendgrid-php/sendgrid-php.php";
function sendgrid() {
$recips = array("me#mydomain.ca");
$categories = array("test");
$sendgrid = new SendGrid("API key removed");
$email = new SendGrid\Email();
$email
->setSmtpapiTos($recips)
->setFrom('mailroom#mydomain.ca')
->setSubject('Testing Sendgrid')
->setText('Hello World! Testing...')
->setHtml('<strong>Hello World!</strong>')
->setCategories($categories)
->setAttachment('test.txt')
;
//$sendgrid->send($email);
$res = $sendgrid->send($email);
var_dump($res);
}
sendgrid();
As far as I can tell, I'm following the documentation, but I wonder if I haven't formatted the path to the file correctly. "Test.txt" is in the same directory as the file that contains the above code.
Can anyone offer any suggestions?
try this
->setAttachment(realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'test.txt');
this doesn't really belong here but as of April 17th 2019 SendGrid broke their postfix mechanism and enforced v3 PHP client without "proper" documentation. And I mean: nothing is properly documented so (since this post came up top of google when I typed in SendGrid - here goes)
so if you see this.. do not panic this post will help u
invalid authentication method - declined because you are using basic authentication with 2FA enabled. to fix, update to using an API key or disable 2FA and switch to using IP Access Management for security.
Some problems with the docs
SendGrids docs on v3 are (in my opinion)
declarative rather than informative (a functional declaration rather than technical information)
cripplingly wrong (wild goose chase of circular links)
out of date (with nothing to alert you)
missing essential components (so won't work out of the box with no hint as to why) ....
for example : (just a flavour) here we see "they" (SendGrid) simply took out \SendGrid\Email() and replaced it with \SendGrid\Mail\Mail() in their code base but didn't update their docs - so their posted examples will not work. - it's very minor as a change - but as written the callers are different and they never updated their examples.. with everything else they omitted it is making very hard work of a easy thing.
i.e. THIS EXAMPLE WORKS v3
$sendgrid = new SendGrid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
$email = new \SendGrid\Mail\Mail();
$email->setFrom("test#example.com", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("bigman#getme.com", "Example User");
$email->addContent(
"text/plain", "and easy to do anywhere, even with PHP"
);
$email->addContent(
"text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
//optional (seems to be bullet proof, pdf, png, etc) to attach a file<START>
$att1 = new \SendGrid\Mail\Attachment();
$att1->setContent(file_get_contents("/path/to/some/test_attach.txt"));
$att1->setType("application/octet-stream");
$att1->setFilename(basename("/path/to/some/test_attach.txt"));
$att1->setDisposition("attachment");
$email->addAttachment($att1);
//optional to attach a file</END>
try {
$response = $sendgrid->send($email);
print $response->statusCode() . "\n";
print_r($response->headers());
print $response->body() . "\n";
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}`
TO UPGRADE TO v3 SEND GRID PHP CLIENT without composer on PHP 5.6
if you're like me and hate composer (don't even get me started) THE GOOD NEWS IS you can get SendGrid to work so- just so you know .. almost everything "they" (SendGrid) direct you to online is going to struggle to help you
After April 17th 2019 SendGrid said you NEED to use v3 YOU WILL NEED PHP 5.6. (rightly or wrongly PHP 5.4 is the ceiling of redhat/centos at time of writing) and YOU CANNOT USE postfix anymore (despite them never specifically saying this nor labeling their postfix examples v2 max, i.e. disabled as of 17th April 2019)
So as of 17th April 2019
-> you have to use v3
-> you have to use the PHP client... (no postfix)
-> you have to upgrade to 5.6
-> you **DO NOT** have to use composer (but if you don't you need to pay attention, see below)
ALSO -> (IMPORTANT) you are stuck with the client or the curl at v3 (they don't simply say this) no postfix. forget it. also re: the Curl mechanism (while it does work with 5.4) carries a) no documentation on using attachments AND b) utilizes illegal json_encoding PHP cannot output ( PHP cannot give "{[{}]}" it's always "{"0":[{}]}")
but regardless not only MUST you use the SendGrid PHP client at 5.6 PHP (again no, server postfix option) you still need to know you need TWO projects: "php-http-client-master" and "sendgrid-php-master" (BOTH repos) and this is undocumented: then you need to do the following: and the /lib/loader.php in "sendgrid-php-master" needs these two lines adding at the end
require_once '/your/path/to/php-http-client-master/lib/Client.php';
require_once '/your/path/to/php-http-client-master/lib/Response.php';
you also need to add this (sadly more sloppiness from SendGrid)
require_once __DIR__ . '/mail/TypeException.php';
Incidentally I also edited sendgrid-php-master\sendgrid-php.php to this
<? php
require __DIR__ . '/lib/loader.php';
?>
theoretically adding composer would mean you could probably avoid all this but for some servers this is out of the question (implementing composer on a different branch of my PHP project took 3 weeks - I am not repeating this) since composer introduces a breaking change in the way PHP handles includes and class autoloading
PS: I have alerted SendGrid to all of this you may well find that this is cleared up pretty fast
Now that the code for a small RSS-reading CMS that I've done is hosted on github, I'd like it to
check automatically if there is a newer version of itself's in the master branch
Allow the user to update said script, ie it would overwrite itself with the newer version
Here is what I came up with (thanks to cillosis's answer)
$commits = json_decode(file_get_contents("https://api.github.com/repos/user_name/repository_name/commits"));
$current_commit_minus1 = $commits[1]->sha;
$ref_commit = "57b75c0f8aefa5ce87c1270265cace18a2347594";
if (!strcmp($current_commit_minus1, $ref_commit))
$moved = true;
else
$moved = false;
That way I don't have to maintain tags, just compare commits.
It should be possible maintaining a current version number within the script, and then comparing it to the repository using the Repositories API.
You can get repo tags with CURL like this (replace :user and :repo with your stuff):
curl http://github.com/api/v2/json/repos/show/:user/:repo/tags
You can show branches like this:
curl http://github.com/api/v2/json/repos/show/:user/:repo/branches
There is a lot of other info available in that API as well.
Once you get that, compare it to the current and procede with updating.
So I'm trying to tailor PHP's Tidy to my liking, but the problem is with the tidy_setopt() function.
I know tidy is installed and working just fine, and reading the PHP docs it says tidy_setopt() has been removed as of 2.0 (So since the ob callback is working perfectly I'm safe to assume I'm running Tidy 2.0+).
Here is the problem: There is no alternative function. I'm hoping there is a way to get around this so I can set the ob handler's settings up how I want them to without actually needing to edit a configuration file.
I'm sure my hosting will be willing to edit Tidy's configuration file if needed, but I'd rather not add to the barrage of support tickets I've been sending them for various reasons as it is.
If I need to create my own callback for output buffering I can do so (I see some possibly useful methods using the OO approach to tidy) but I'd rather have it as slim as possible.
Instead of using
tidy_setopt('indent', FALSE);
You should use
$config = array('indent' => FALSE);
$text = tidy_parse_string($text, $config, 'UTF8');
Also see Output Control Functions manual for "User defined callback function example"