Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
15
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@tukiyo3

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

参考

準備

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);
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
15
Help us understand the problem. What are the problem?