I try update a YouTube Video. I use the function at the site https://developers.google.com/youtube/v3/code_samples/php#update_a_video and I try to copy this in my code but if I call listVideos(), I get Google_Service_YouTube_VideoListResponse but I need a Google_Service_YouTube_Video. How can I get it?
My PHP Code:
$youtube = new Google_Service_YouTube($client);
$videoid = "******Video ID********";
$new = "******* TEXT *********";
$videoinfo = $youtube->videos->listVideos('snippet', array('id' => $videoid));
$videoSnippet = $videoinfo[0]['snippet'];
$description = $videoSnippet['description'] . $new;
$videoSnippet['description'] = $description;
$youtube->videos->update("snippet", $videoSnippet);
The Error:
: Uncaught TypeError: Argument 2 passed to
Google_Service_YouTube_Resource_Videos::update() must be an instance of
Google_Service_YouTube_Video, instance of
Google_Service_YouTube_VideoSnippet given, called in
P:\Apache24\htdocs\*********** on line 232 and defined in
P:\Apache24\htdocs\*******\Google Api System\vendor\google\apiclient-
services\src\Google\Service\YouTube\Resource\Videos.php:309
Stack trace:
0 P:\Apache24\htdocs\*********: Google_Service_YouTube_Resource_Videos-
>update('snippet', Object(Google_Service_YouTube_VideoSnippet ))
1 P:\Apache24\htdocs\********\*********\index.php(438):
require('P:\\Apache24\\htd...')
you have to use
$video = $listResponse[0];
$updateResponse = $youtube->videos->update("snippet", $video);
since in the documentation
// Since the request specified a video ID, the response only
// contains one video resource.
$video = $listResponse[0];
$videoSnippet = $video['snippet'];
$tags = $videoSnippet['tags'];
// Preserve any tags already associated with the video. If the video does
// not have any tags, create a new list. Replace the values "tag1" and
// "tag2" with the new tags you want to associate with the video.
if (is_null($tags)) {
$tags = array("tag1", "tag2");
} else {
array_push($tags, "tag1", "tag2");
}
// Set the tags array for the video snippet
$videoSnippet['tags'] = $tags;
// Update the video resource by calling the videos.update() method.
$updateResponse = $youtube->videos->update("snippet", $video);
Related
I need to obtain a file's meta-data from just using the Google Drive FileID only. The assumption being that I don't have knowledge of the folderID. By just using the FileID the API just returns null for the filesize but the correct values for the other meta-data.
Just to be clear, I can get all the meta data (including filesize) by processing the files within a folder, for example:
<?php
/*
* With this example I have the folderID from a Google Drive shared link
*/
$folderId = "https://drive.google.com/drive/folders/1ggWcacF9qQroZOkhfb3tTEwvBzclccwI?usp=share_link";
$Auth = new Oauth2Authentication();
$client = $Auth->GetClient();
$client = $Auth->initializeClient($client);
$drive = new DriveService($client);
$pattern = "/[-\w]{25,}(?!.*[-\w]{25,})/";
if (preg_match($pattern, $folderId, $match)); // grabs folderID from the url
/*
* Now list the folder contents
*/
$responseFiles = $drive->ListFiles($match[0]);
$files = $responseFiles['files'];
foreach ($files as $file) {
$id[] = $file->id;
$name[] = $file->name;
$mimeType[] = $file->mimeType;
$size[] = $file->size; //<- correct file size
};
$drive->DownloadFile($id, $name, $size);
The code below, which I need to work, returns most of the meta-data except that, as stated above, the $file->size is always null. My DownloadFile method needs the file size so that it can better handle large files. Is there anyone who can help me better understand the problem?
<?php
/*
* With this example I have the only the fileID from a Google Drive shared link
*/
$fileId = "https://drive.google.com/file/d/1EjNk1ijPLKJMwXfzkEIG487HFzx0v80v/view?usp=share_link";
$Auth = new Oauth2Authentication();
$client = $Auth->GetClient();
$client = $Auth->initializeClient($client);
$drive = new DriveService($client);
$pattern = "/[-\w]{25,}(?!.*[-\w]{25,})/";
if (preg_match($pattern, $fileId, $match)); // grabs fileID from the url
/*
* Now get the file meta data and download the file
*/
$fileId = $match[0];
$service = new Google_Service_Drive($client);
$file = $service->files->get($fileId);
$drive->DownloadFile($file->id, $file->name, $file->size); //$file->size is always null
Update: Thanks to #DaImTo I was pointed in the right direction. The full fileID's details can be obtained by adding
array('fields' =>'*')
to the argument list of the $service->files->get method, i.e.
$file = $service->files->get($fileId,array('fields' =>'*'));
Files.list retuns a list of files with limited response.
files": [
{
"kind": "drive#file",
"mimeType": "application/vnd.google-apps.spreadsheet",
"id": "1x8-vD-X2wltablGF22Lpwup8VtxNY",
"name": "Experts Activity Dump go/ExpertsActivities"
},
To get the file size either tweek the optimal parameter fields or just use .
Fileds=*
or
$responseFiles = $drive->ListFiles($match[0],array('fields' =>'*'))
I'm making a web crawler, but I'm getting an error because I can't use more than one dom element. I think I need to manipulate the dom element, but I have no idea how to do it.
Im using Symfony DomCrawler and Sunra PhpSimple HtmlDomParser
Code:
$crawler = $this->crawler;
$crawler->addHtmlContent(HtmlDomParser::file_get_html($url, false, null, 0));
// Getting the URL data
$crawler
->filter('a')
->each(function (crawler $node) use ($url): void {
$url_fr_hrf = $node->attr('href');
if(str_starts_with($url_fr_hrf, '/') OR str_starts_with($url_fr_hrf, '#')): $url_fr_hrf = $url . $node->attr('href'); endif;
$this->datas = [
'url' => $url_fr_hrf,
];
// Checking Urls
if(substr_count($this->datas['url'], '/') > 4 && parse_url($this->datas['url'], PHP_URL_HOST) === parse_url($url, PHP_URL_HOST)):
// Not searcing for the under links
else:
$check = $this->db->db->prepare("SELECT * FROM crawler WHERE url = ?");
$check->execute([$this->datas['url']]);
$check_f = $check->fetch(PDO::FETCH_ASSOC);
if($check_f['url'] === $this->datas['url']):
// Url already exists
else:
$insert = $this->db->db->prepare("INSERT INTO crawler SET url = ?");
$insert->execute([$this->datas['url']]);
endif; endif;
$this->url = $this->datas['url'];
sleep(0.5);
});
//echo $url . PHP_EOL;
$ins = $this->db->db->prepare("SELECT * FROM crawler"); $ins->execute();
while ($links = $ins->fetch(PDO::FETCH_ASSOC)):
$this->request($links['url']);
endwhile;
Error: Uncaught InvalidArgumentException: Attaching DOM nodes from multiple documents in the same crawler is forbidden. in...
Please help me solve this error
I would like to get all my youtube watch later video list whith PHP API 3.0 but it seem's that we can not access to these with PHP and API 3. Does anyone have done it yet ?
My code for now
// Call the YouTube Data API's channelSections.list method to retrieve your channel sections.
$listResponse = $youtube->channelSections->listChannelSections('snippet,contentDetails', array('mine' => true));
$channelSections = $listResponse['items'];
$htmlBody .= "<h2>Sections Shuffled</h2><ul>";
foreach ($channelSections as $channelSection) {
// Each section in the list of shuffled sections is sequentially
// set to position 0, i.e. the top.
$channelSection['snippet']['position'] = 0;
// Call the YouTube Data API's channelSections.update method to update a channel section.
$updateResponse = $youtube->channelSections->update('snippet,contentDetails', $channelSection);
$htmlBody .= sprintf('<li>%s "%s"</li>',
$updateResponse['id'], $updateResponse['snippet']['title']);
}
$htmlBody .= '</ul>';
Use $youtube->channels->listChannels instead of $youtube->channelSections->listChannelSections
// Get all channels of current user
$channels = $youtube->channels->listChannels('contentDetails', ['mine' => true]);
// Get watch later playlists ID from the first channel
$watchLaterListId = $channels[0]->getContentDetails()->getRelatedPlaylists()->getWatchLater();
// Get videos from the playlist
$videos = $youtube->playlistItems->listPlaylistItems('contentDetails', ['playlistId' => $watchLaterListId]);
// Loop videos
foreach($videos as $video)
{
// This is the global video ID.
$globalVideoId = $video->getContentDetails()->getVideoId();
// This is unique ID of this video on the playlist. The one you need to interact with the playlist
$playlistVideoId = $video->id;
//i.e: To remove this video from the playlist
$youtube->playlistItems->delete($playlistVideoId);
}
Is it possible to grab the url Netsuite uses for an item image using the PHP Toolkit? Using this function:
function getProduct($id) {
$service = new NetSuiteService();
$service->setSearchPreferences(false, 1000);
$itemInfo = new SearchMultiSelectField();
$itemInfo->operator = "anyOf";
$itemInfo->searchValue = array('internalId' => $id);
$search = new ItemSearchBasic();
$search->internalId = $itemInfo;
$request = new SearchRequest();
$request->searchRecord = $search;
$searchResponse = $service->search($request);
$products = $searchResponse->searchResult->recordList->record;
return $products;
}
I can get the item information. In the information, netsuite gives me this little snippet (the whole thing is truncated for cleanliness):
...
[storeDisplayImage] => RecordRef Object
(
[internalId] => 19876
[externalId] =>
[type] =>
[name] => 4010024.jpg
)
...
This just tells me the name of the file in the Netsuite file cabinet. How do I download that image automatically using the PHP Toolkit?
Try this:
$request2 = new GetRequest();
$request2->baseRef = new RecordRef();
$request2->baseRef->internalId = $product->storeDisplayImage->internalId;
$request2->baseRef->type = "file";
$getResponse2 = $this->service->get($request2);
if (!$getResponse2->readResponse->status->isSuccess) {
echo "GET ERROR";
} else {
$file = $getResponse2->readResponse->record;
echo "GET SUCCESS, File name is " . $file->name;
echo "<img src='".$file->url."'>";
}
I think you will want to look at:
["storeDisplayImage"]=>
object(RecordRef)#20 (4) {
["internalId"]=>
string(6) "758653"
["externalId"]=>
NULL
["type"]=>
NULL
["name"]=>
string(26) "image_name.jpg"
}
I cannot find any way to actually access the image, but if it is possible, the internalId (758653) is what you should be using to search. It is used when accessing the image via URL:
https://system.netsuite.com/core/media/media.nl?id=758653&c=ACCOUNT#&h=31578b297cc0be65db1f&expurl=T
The problem is that random hash at the end of the URL. I can't quite figure out what it is, but if you could, you would be able to construct that URL.
EDIT: nlapiResolveURL might help you. Try passing the internalId to nlapiResolveURL and it could provide that URL dynamically.
I'm trying to get the video data from this youtube playlist feed and add the interesting data to an array and use that later, but as you can see from the feed some videolinks are "dead" and that results in problems for my code.
The error I get is "Node no longer exists" when I try to access $attrs['url']. I've tried for hours to find a way to check if the node exists before I access it but I have no luck.
If anyone could help me to either parse the feed some other way with the same result or create a if-node-exists check that works I would be most happy. Thank you in advance
$url = 'http://gdata.youtube.com/feeds/api/playlists/18A7E36C33EF4B5D?v=2';
$sxml = simplexml_load_file($url);
$i = 0;
$videoobj;
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$videoobj[$i]['url'] = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = $attrs['url'];
$videoobj[$i]['title'] = $media->group->title;
$i++;
}
if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) {
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = strval($attrs['url']);
$videoobj[$i]['title'] = strval($media->group->title);
}
SimpleXML's methods always return objects, which are themselves linked to the original document (some internal thingy related to libxml.) If you want to store that data for later use, cast it as a string, like this:
$videoobj[$i]['url'] = (string) $attrs['url'];
$videoobj[$i]['thumb'] = (string) $attrs['url'];
$videoobj[$i]['title'] = (string) $media->group->title;