4

I've been trying to use curl to send requests but I keep getting an error:

Error: "" - Code: 3 (The URL was not properly formatted.)

Here is my code that I have been using (after successful authentication and using my access token)

$name = 'test' . rand();
$notes = 'hello hello' . rand();
// Curl request to create asana task.
$url = 'https://app.asana.com/api/1.0/tasks';
$header = "Authorization: Bearer $token";

$curl = curl_init();
curl_setopt($curl, array(
  CURLOPT_URL => $url,
  CURLOPT_POST => 1,
  CURLOPT_POSTFIELDS => array(
    'notes' => $notes,
    'name' => $name,
    'projects' => 'xxxxxxxxxxxxxxx',
  ),
));
curl_setopt($curl, CURLOPT_HTTPHEADER, array($header));
$result = curl_exec($curl);

if(!curl_exec($curl)){
  die('Error: "' . curl_error($curl) . '" - Code: ' .    curl_errno($curl));
}

Where I got the project id from the id in the URL of the project.

Update: the equvilant command line curl command I took from https://asana.com/developers/api-reference/tasks and is below. Running this is in the command line worked.

curl -H "Authorization: Bearer tokenid123"  https://app.asana.com/api/1.0/tasks  -d "notes=test notes"  -d "projects[0]=xxxxxxxxxxxxxxx"  -d "name=test"
farse311
  • 53
  • 4
  • Print out the command you generate and try it on the console. Add the command and the results to your question. – J Fabian Meier Jul 07 '16 at 11:03
  • Thanks I've updated the post. The command line curl worked (no errors), but still can't see what is wrong with the php version. – farse311 Jul 07 '16 at 12:46
  • I found an error in my code.. I needed `curl_setopt_array` instead of `curl_setopt` have a different set of errors now (not authenticated for some reason) but at least I can work with that now. – farse311 Jul 07 '16 at 13:02
  • On further inspection my access token was slightly not in the correct format (add extra " " it didn't need surrounding it) and so everything is honky dory now. Thanks ether. – farse311 Jul 07 '16 at 13:12
  • Why don't you wanna use asana library?https://github.com/Asana/php-asana – Oyeme Jul 07 '16 at 13:13
  • You're oh so right Oyeme. I used the library for authentication and for some reason that's all I thought it did, I got too caught up in reading the API reference on the site (it looked easy enough). At least I've learned a little bit more of curl now. – farse311 Jul 07 '16 at 13:23

0 Answers0