12
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

phpのcliからcurlでファイルアップロード

Last updated at Posted at 2016-03-03

参考

準備

apt-get install -y php5-curl

ファイルアップロード

送信側(ubuntu14.04のphp5.5.9で確認)

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL         => 'http://myserver/upload.php',
    CURLOPT_POST        => true,
    CURLOPT_POSTFIELDS  => [
        'userfile[]' => new CURLFile('./a.txt'),
    ],
]);
curl_exec($ch);

受け側

$uploaddir = '/tmp/';

$file = $_FILES['userfile'];
$uploadfile = $uploaddir . basename($file['name'][0]);

if (move_uploaded_file($file['tmp_name'][0], $uploadfile)) {
    echo "upload successfully.\n";
} else {
    echo "upload failed!\n";
}
  • /tmp/a.txt が生成されていれば成功。

GET

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://myserver/get.php?a=1&c=2');
curl_exec($ch);
curl_close($ch);

POST(古い書き方)

$data = array(
        'test1' => 'aaa',
        'test2' => 'bbb'
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://myserver/post.php');
$result = curl_exec($ch);
echo 'RETURN:'.$result;
curl_close($ch);
12
17
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
12
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?