Auguste Larrivé
Auguste Larrivé

Reputation: 15

Issues calling Youtube Api via Curl: CURLE_URL_MALFORMAT error

I'm trying to delete a video by its ID via YoutubeAPi using curl.

In the following code curl_errno() returns CURLE_URL_MALFORMAT error.

$link = urlencode("https://www.googleapis.com/youtube/v3/videos?id=my_Video_ID&key=my_API_KEY");

 $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);
    $test = curl_errno($ch);
    echo $test . "\n";
    $result = json_decode($result);
    curl_close($ch);
    return $result;

What's wrong with the code ?

Upvotes: 1

Views: 433

Answers (1)

Ruslan Osmanov
Ruslan Osmanov

Reputation: 21492

You don't need to encode the URL (urlencode). Just assign the URL string to your $link variable. (Read the docs.)

However, it is good idea to encode the parameter values:

test.php

$id = urlencode('AAAAABBBCCC');
$key = urlencode('AAAAAAAAABBBBBBBBBBBBBCCCCCCCCCCCCCCCCC');
$link = "https://www.googleapis.com/youtube/v3/videos?id=$id&key=$key";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
var_dump($result);

Testing

php test.php

Output

string(238) "{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}
"

The API actually works. Now you need to set the correct id and key parameters and the authorization token as described in the official docs:

You must send an authorization token for every insert, update, and delete request.

Upvotes: 1

Related Questions