PHP file_get_contents and Asana API

Hi everyone,

I’m trying to retrieve some value from our asana workspace.

I’ve been able to do it with javascript

	var bearerToken = "bearer APIKEY";
    var requestUrl = "https://app.asana.com/api/1.0/projects/PROJECTID/tasks"
     var headers = {
      "Authorization" : bearerToken

	};
	
	var reqParams = {
		method : "GET",
		headers : headers,
		muteHttpExceptions: true
	  };

	let res= await fetch(requestUrl,reqParams); // (2)

But for safety reason, i’m trying to convert this code to PHP so :

$url = 'http://app.asana.com/api/1.0/projects/PROJECTID/tasks';


$opts = [
    "http" => [
        "method" => "GET",
        "header" => "Content-Type: application/json\r\n" .
            "charset=utf-8\r\n" ,
            "Authorization : bearer APIKEY \r\n"
    ]
];

$context = stream_context_create($opts);


$file = file_get_contents('app.asana.com/api/1.0/projects/1158939083333529/tasks', false, $context);
    
$file = file_get_contents($url, false, $context);
$json_echo = json_decode($file)
 echo $json_echo;

The echo will return empty value …

Best regards

Darren

Here a full functional solution with cURL :slight_smile:

$apikey = 'your_api_key_here'

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, “https://app.asana.com/api/1.0/projects/your_project_id/tasks”); # URL to post to
curl_setopt($ch, CURLOPT_USERPWD, $apikey); #apikey
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 ); # return into a variable
curl_setopt($ch, CURLOPT_HTTPHEADER, $header ); # custom headers, see above
$result = curl_exec( $ch ); # run!
curl_close($ch);
echo $result;