LoginSignup
1
0

More than 5 years have passed since last update.

Bluemix の Cloudant から、 PHP で画像ファイルをダウンロード

Posted at

次のページで行うことと同じことを、PHP で行いました。
Bluemix の Cloudant から、python3 で画像ファイルをダウンロード
Bluemix の Cloudant から、 Node.js で画像ファイルをダウンロード

from_cloudant.php
#! /usr/bin/php
<?php
// ------------------------------------------------------------------
//  from_cloudant.php
//
//                  Jul/27/2017
//
// ------------------------------------------------------------------
function curl_get_proc ($url)
{
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt ($ch, CURLOPT_PROXY, "");

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;
}

// ------------------------------------------------------------------
function file_write_proc ($string_out,$file_out)
{
    $fp_out=fopen ($file_out,"w");
    flock ($fp_out,LOCK_EX);
    fputs ($fp_out,$string_out);
    flock ($fp_out,LOCK_UN);
    fclose ($fp_out);

    chmod ($file_out,0666);
}

// ------------------------------------------------------------------
// [6]:
function download_jpg_proc($url_collection,$key)
{
    $file_jpg= $key . ".jpg";

    $url_jpg = $url_collection . "/" . $key . "/" . $file_jpg;

    $image = curl_get_proc ($url_jpg);

    file_write_proc ($image,$file_jpg);
}

// ------------------------------------------------------------------
// [2]:
function list_keys_proc($url_collection)
{
    $url_all_docs = $url_collection . "/_all_docs?include_docs=true";

    $json_string = curl_get_proc ($url_all_docs);

    $list_aa = json_decode ($json_string,true);

    $rows = $list_aa['rows'];

    $keys= array ();
    foreach ($rows as $unit)
        {
        $keys[] = $unit['key'];
        }

    return $keys;
}

// ------------------------------------------------------------------
print   "*** 開始 ***\n";

$url="https://44b508dd-f332-4f91-81f2-78369c7d29d9-bluemix:e44b508dd666c2a5c16596fc371012e08a61e7132d3b21ecaaf1f63a835fae64@efc189dc-f332-4f91-81f2-2969c7d29d9-bluemix.cloudant.com";
#
$url_collection = $url . '/jpg';
print   "url_collection = " .$url_collection . "\n";

$keys = list_keys_proc($url_collection);

foreach ($keys as $key)
    {
    print $key . "\n";
    download_jpg_proc($url_collection,$key);
    }

print   "*** 終了 ***\n";

// ------------------------------------------------------------------
?>
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0