daviddesberg / PHPoAuthLib

PHP 5.3+ oAuth 1/2 Client Library
Other
1.08k stars 454 forks source link

Support to upload Flickr Images #577

Open FraGoTe opened 3 years ago

FraGoTe commented 3 years ago

I'm adding a feature to upload Flickr images directly from this lib.

Usage:

$flickrService = $serviceFactory->createService('Flickr', $credentials, $storage, [],
    new OAuth\Common\Http\Uri\Uri('https://up.flickr.com/services/upload/')
);

$multipart = '--------------------------'  .  microtime(true);
$fileName = __DIR__  .  '/img.png';
$fileBin = file_get_contents($fileName);

$content =  "--" . $multipart."\r\n".
"Content-Disposition: form-data; name=\"photo\"; filename=\"".basename($fileName)."\"\r\n".
"Content-Type: application/png\r\n\r\n".
$fileBin."\r\n";

// signal end of request (note the trailing "--")
$content .= "--" . $multipart . "--\r\n";

$req = $flickrService->request(
  '/',
  'POST',
  $content,
  ['Content-Type' => 'multipart/form-data; boundary=' . $multipart]
);

$x = simplexml_load_string($req);
$photoId = (string)$x->photoid;
var_dump('<pre>', $x, (string)$x->photoid);