How could I update a youtube video via php api? - php

I will update a video with a code like this:
$headers = array("PUT /feeds/api/users/default/uploads/VIDEOID HTTP/1.1",
"Host: gdata.youtube.com",
"Authorization: GoogleLogin auth=".$authvalue,
"GData-Version: 2",
"X-GData-Key: key=KEY",
"Content-length: ".strlen($data),
"Content-Type: application/atom+xml; charset=UTF-8");
$curl = curl_init("https://gdata.youtube.com/feeds/api/users/default/uploads/VIDEOID");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_REFERER, true);
curl_setopt($curl, CURLOPT_HEADER, 0);
$returnxxx = curl_exec($curl);
curl_close($curl);
echo $returnxxx;
I've generate a valide authvalue (tested it -> ok). But it shows:
invalid uri
Thanks for helping!

#Marc B Thanks for your hint!
I've used curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); instead of the curl_setopt($curl, CURLOPT_POST, 1);!

Related

Curl PHP to API with HTTP Header and Post data conflicting

I am making a curl call using PHP to a PHP based URL and getting into some problems.
Please note that the when I add the Content-Type in the header, it causes the endpoint not to read my params at all.
Any help is appriciated.
Here is what I am doing and the result I am getting:
With Header but no Content-Type in Header:
$curl = curl_init();
$params="action=start_day_activity&woid=23";
$headers = [
"Authorization: Bearer $token"
];
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($curl);
// Status 200
// $result becomes = "{"flag":0,"message":"Token not found in request","data":[]}"
With Header with Content-Type in Header:
*$curl = curl_init();
$params="action=start_day_activity&woid=23";
$headers = [
'Content-Type: application/json',
"Authorization: Bearer $token"
];
curl_setopt($curl, CURLOPT_URL, $api_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($curl);*
// Status 200
// $result becomes = "{"flag":0,"message":"No Request.","data":[]}"

cURL PUT is returning only bool(false) with PHP

I am using the following code:
<?php
$url = "https://api.challonge.com/v1/tournaments/hamiltonjenn/matches/245771359.json";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Accept: application/json",
"Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{"match":{"player1_votes": 4}}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
?>
var_dump shows only bool(false)
Any ideas why this is happening? I cannot determine the reason this won't work.
I'm not sure why, but changing the curl_setopt($curl, CURLOPT_PUT, true); to curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); solved the issue. I'm gathering that CURLOPT_PUT, true is expecting a file, whereas CURLOPT_CUSTOMREQUEST, "PUT" is expecting an update to the string.

PHP Rest API Call with Authorization

Simple question
Where would I put "Authorization: Bearer cajwune0fnr78ynj2kz6p8bad"
$service_url = 'https://example.com/something/something.json';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);
var_dump($response);
Thanks!
You'd put it into:
***old
curl_setopt($curl, CURLOPT_HTTPHEADER, 'Authorization: Bearer cajwune0fnr78ynj2kz6p8bad');
***new
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Bearer cajwune0fnr78ynj2kz6p8bad'));

Associating an unlinked Google Account with a YouTube channel

How do you implement https://developers.google.com/youtube/2.0/developers_guide_protocol_deprecated using PHP?
What I tried:
<?php
$headers = array("PUT /feeds/api/users/default HTTP/1.1",
"Host: gdata.youtube.com",
"Content-Type: application/atom+xml",
"Authorization: Bearer ACCESS_TOKEN",
"X-GData-Key: key=DEVKEY",
"Content-length: ".strlen($data),
$curl = curl_init("https://gdata.youtube.com/feeds/api/users/default?v=2.1");
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_REFERER, true);
curl_setopt($curl, CURLOPT_HEADER, 0);
$returnxxx = curl_exec($curl);
curl_close($curl);
echo $returnxxx;
?>
I am missing ACCESS_TOKEN though.
Associating an unlinked Google Account with a YouTube channel is no longer possible via the APIā€”that's why it's in the deprecated section of the documentation.
The current way of prompting a user to link a Google Account to a channel is described at https://developers.google.com/youtube/create-channel That guide is mobile-focused, but the same process can be kicked off by redirected a user to https://www.youtube.com/create_channel on the desktop.

convert command curl to php

Hi all! I have curl command (audio file upload):
curl -k -v -H "Expect: " -H "Content-Type:application/octet-stream" --data-binary '#/Path/To/File/test.wav' -X POST 'https://myapi.com?param1=xxx&param2=yyy'
File successfully uploaded and readable. But when I used php script:
$filename = 'test.wav';
$file = '#/Path/To/File/test.wav';
$post_url = "https://someapi.com?param1=xxx&param2=yyy";
$post_str = array("$filename" => $file);
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/octet-stream'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$http_body = curl_exec($ch);
var_dump($http_body);
File successfully uploaded and test.wav is not valid (with some error). What am I doing wrong in the script?
I suspect the issue is these lines:
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/octet-stream'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect: '));
The second call will remove the "Content-Type" Header. Try consolidating these:
$headers = array('Content-Type:application/octet-stream','Expect: ');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$filename = 'test.wav';
$file = '/Path/To/File/test.wav';
$post_url = "https://someapi.com?param1=xxx&param2=yyy";
$post_str = file_get_contents($file);
$headers = array('Content-Type:application/octet-stream', 'Expect: ');
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $post_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch,CURLOPT_VERBOSE,true);
curl_setopt($ch, CURLOPT_STDERR, fopen("/Path/to/header.txt", "w+"));
$http_body = curl_exec($ch);
It's work perfect. I solved the problem myself. File upload to the server binary. Thank you all!

Categories