# php curl 使って クリックなしで POST 送信
サンプルデータは配列
// 転送先
$url = "https://google.com";
// 転送用フォームデータ
$data = [
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
'key4' => 'value4',
];
# Function
public function curlData($url,$data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
if(! $result = curl_exec($ch) ){
echo curl_error($ch);
}
curl_close($ch);
return $result;
}
# curl_init()
cURL セッションを初期化する
説明
curl_init ([ string $url = NULL ] ) : resource
PHP リファレンス curl_init (opens new window)
# curl_setopt()
転送用オプション設定
説明
curl_setopt ( resource $ch , int $option , mixed $value ) : bool
# オプション
| オプション | 設定値 |
|---|---|
| CURLOPT_URL | 取得する URL |
| CURLOPT_CUSTOMREQUEST | "GET"、 "POST"、"CONNECT" など |
| CURLOPT_POSTFIELDS | 送信するデータ |
| CURLOPT_RETURNTRANSFER | TRUE を設定すると、curl_exec() の返り値を 文字列で返します。通常はデータを直接出力します。 |
| CURLOPT_HTTPHEADER | ヘッダフィールドの配列 ['Content-type: text/plain', 'Content-length: 100'] |
CURLOPT_POSTFIELDS
PHP 5.5.0 以降、@ プレフィックスは非推奨
PHP リファレンス curl_setopt (opens new window)
# curl_exec()
指定した URL に data 送信
説明
curl_exec ( resource $ch ) : mixed
PHP リファレンス curl_exec (opens new window)
# curl_close()
システムリソースを解放
説明
curl_close ( resource $ch ) : void