Search code examples
phpfile-get-contents

PHP file_get_contents and Asana API


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


Solution

  • Here a full functional code for anyone that might requires it:

    $WCURL = 'https://app.asana.com/api/1.0/projects/projetc_id/tasks';
    $CURLHTTPHeader = array(
        'Content-Type: application/json',
        'Accept: application/json',
        'Authorization: Bearer your_api_key');
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,$WCURL);
    //curl_setopt($ch, CURLOPT_POST, 1);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$CURLHTTPHeader);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $curlResponse = curl_exec ($ch);
    curl_close ($ch);
    
    echo $curlResponse;