【php】curl使い方メモ - GET,POST,PUT,DELETEとJSON

今更な内容だけど。たまにcurlで外部のAPI叩く必要があるときにいつも同じこと調べてる気がするのでコピペ用にまとめておく。
GET、POST、PUT、DELETEメソッドでリクエストして、パラメータがちゃんと送れているか確認するためのスクリプト。ついでにパラメータをJSONで送る場合も。

受信側
$method = $_SERVER['REQUEST_METHOD'];
$content_type = $_SERVER['CONTENT_TYPE'];

if ($content_type == 'application/json')
{
    $params = json_decode(file_get_contents('php://input'),1);
}
else if ($method == 'GET')
{
    $params = $_GET;
}
else if ($method == 'POST')
{
    $params = $_POST;
}
else if ($method == 'PUT')
{
    $params = file_get_contents('php://input');
}
else if ($method == 'DELETE')
{
    $params = file_get_contents('php://input');
}

print_r(array(
    'method' => $method,
    'content_type' => $content_type,
    'params' => $params,
));
送信側

GET

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => sprintf(
        '%s?%s', API_URL,
        http_build_query(array(
            'param1' => 'パラメータ1',
            'param2' => 'パラメータ2',
        ))
    ),
    CURLOPT_RETURNTRANSFER => true,
));

$response = curl_exec($curl);
$info = curl_getinfo($curl);

curl_close($curl);

print_r(array(
    'http_code' => $info['http_code'],
    'response' => $response,
));

POST

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => API_URL,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => array(
        'param1' => 'パラメータ1',
        'param2' => 'パラメータ2',
    ),
));

$response = curl_exec($curl);
$information = curl_getinfo($curl);

curl_close($curl);

print_r(array(
    'http_code' => $information['http_code'],
    'response' => $response,
));

POST(JSON)

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => API_URL,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => json_encode(array(
        'param1' => 'パラメータ1',
        'param2' => 'パラメータ2',
    )),
    CURLOPT_HTTPHEADER => array(
        'Content-Type:application/json',
    ),
));

$response = curl_exec($curl);
$information = curl_getinfo($curl);

curl_close($curl);

print_r(array(
    'http_code' => $information['http_code'],
    'response' => $response,
));

PUT

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => API_URL,
    CURLOPT_CUSTOMREQUEST => 'PUT',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => http_build_query(array(
        'param1' => 'パラメータ1',
        'param2' => 'パラメータ2',
    )),
));

$response = curl_exec($curl);
$information = curl_getinfo($curl);

curl_close($curl);

print_r(array(
    'http_code' => $information['http_code'],
    'response' => $response,
));

DELETE

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => API_URL,
    CURLOPT_CUSTOMREQUEST => 'DELETE',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => http_build_query(array(
	'param1' => 'パラメータ1',
	'param2' => 'パラメータ2',
    )),
));

$response = curl_exec($curl);
$information = curl_getinfo($curl);

curl_close($curl);

print_r(array(
    'http_code' => $information['http_code'],
    'response' => $response,
));

これで次回からは早くなるはず。書いたこと忘れてなければ。。以上です