phpapicurlpage-curl

How to write Curl api example into PHP curl


I am using https://apptweak.io api, they didn't mention PHP example in their code example...

please tell me, how to understand and write Curl API example into PHP curl

Here is simple "appteak.com" curl example

curl -G https://api.apptweak.com/ios/applications/284993459/informations.json \
-d country=us \
-d language=en \
-d device=iphone \
-H "X-Apptweak-Key: my_api_key"

Here is other example

$ curl -H "X-Apptweak-Key: my_api_key" \
https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl

How to write these curl in PHP?

i did this in php, after google, but its not work for me.

// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl',
   // CURLOPT_USERAGENT => 'Codular Sample cURL Request',
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array(
        'X-Apptweak-Key' => "my_api_key",
    )
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);


var_dump($resp ); //return false

Solution

  • I find correct answer

    $token = "my_api_key";
    $url = "http://URL.com";
    
    $options = array('http' => array(
        'header' => array("X-Apptweak-Key: $token"),
    ));
    $context = stream_context_create($options);
    
    $result = file_get_contents($url, 0, $context);