参考
準備
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);