Skip to content

Instantly share code, notes, and snippets.

@zillingen
Last active December 8, 2017 07:59
Show Gist options
  • Select an option

  • Save zillingen/0f09002ea03b9f03055d54fb8164dfea to your computer and use it in GitHub Desktop.

Select an option

Save zillingen/0f09002ea03b9f03055d54fb8164dfea to your computer and use it in GitHub Desktop.
PHP cURL usage examples

GET request with headers

Sending HTTP GET request containing Authorization headers to public REST API with PHP cURL

function curlGetRequest() {
    $ch = curl_init();

    $headers = array(
        'Authorization: OAuth XXX,
        'Accept: application/json',
    );

    $options = array(
        CURLOPT_URL => 'https://example-api.com',
        CURLOPT_RETURNTRANSFER => TRUE, // return output to var
        CURLOPT_VERBOSE => FALSE, // show HTTP headers 
        CURLOPT_HTTPHEADER => $headers,
    );

    curl_setopt_array($ch, $options);

    $responseBody = curl_exec($ch);
    $responseInfo = curl_getinfo($ch);
    curl_close($ch);

    if ($responseInfo['http_code'] === 200) {
        return TRUE;
    } else {
        return FALSE;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment