Search code examples
phprestcurlnewrelic

Convert line comand curl to PHP curl from New Relic API


i would like to have some help how to convert a comand line request via curl to the PHP curl. I´m trying to consume the New Relic REST service.

The sample code is like this:

curl -X GET 'https://api.newrelic.com/v2/applications.json' \
     -H 'X-Api-Key:100' -i 

Thanks in advance!


Solution

  • You could use curl_setopt_array() instead of curl command line flags.

    <?php
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://api.newrelic.com/v2/applications.json', //URL to the API
        CURLOPT_HEADER => true, // Instead of the "-i" flag
        CURLOPT_HTTPHEADER => array('X-Api-Key:100') //Your New Relic API key
    ));
    $resp = curl_exec($curl);
    curl_close($curl); 
    print_r($resp);