I use PHP to insert/update the title and description of our video by using youtube-api and I get some problems with it.
1.
When I insert/update title and description of video by using zh-Hant or zh-Hans, I get "500 Backend Error."
However it works when I change zh-Hant and zh-Hans to zh-tw and zh-cn.
500 Backend Error
2.
I get "400 invalidMetadata" when I update a video title by using Italian(it).
I have no idea about this because it's all fine in others languages and it also fine in update video description by using Italian.
400 invalidMetadata
How can I fix these problems?
Here is my code:
public function __construct()
{
$client = new \Google_Client();
$client->setAuthConfig(storage_path('key/youtube_key/client_secret.json'));
$client->setApplicationName("String System");
$client->setScopes('https://www.googleapis.com/auth/youtube.force-ssl');
$client->setAccessType('offline');
$client->setAccessToken(Cache::get('google_auth_token'));
$this->client = $client;
}
public function youtubeService($stringKey, $stringValue, $language)
{
$this->service = new \Google_Service_YouTube($this->client);
$this->stringKey = $stringKey;
$this->stringValue = $stringValue;
$this->language = $language;
$key = explode('_', $this->stringKey);
if (3 != count($key)) {
return;
}
list($videoName, $videoID, $type) = $key;
$this->videoName= $videoName;
$this->videoID = $videoID;
switch ($type) {
case 'title':
$this->updateTitle();
break;
case 'description':
$this->updateDesc();
break;
}
return;
}
private function updateTitle()
{
// get video list by video ID
$video = $this->service->videos->listVideos(
'snippet,localizations',
['id' => $this->videoID]
);
$videoInfo = $video->items[0];
// set video title of language
if (isset($videoInfo->localizations[$this->language])) {
$videoInfo->localizations[$this->language]->title =
$this->stringValue;
// check default language
if ($this->language === $videoInfo->snippet->defaultLanguage) {
$videoInfo->snippet->title = $this->stringValue;
}
} else {
$videoInfo->localizations[$this->language] = [
'title' => $this->stringValue,
'description' => 'description'
];
}
try {
// update video information
$this->service->videos->update(
'snippet,localizations',
$videoInfo
);
} catch (Exception $e) {
// do nothing and continue
}
return;
}
private function updateDesc()
{
// get video list by video ID
$video = $this->service->videos->listVideos(
'snippet,localizations',
['id' => $this->videoID]
);
$videoInfo = $video->items[0];
// set video description of language
if (isset($videoInfo->localizations[$this->language])) {
$videoInfo->localizations[$this->language]->description =
$this->stringValue;
// check default language
if ($this->language === $videoInfo->snippet->defaultLanguage) {
$videoInfo->snippet->description = $this->stringValue;
}
} else {
$videoInfo->localizations[$this->language] = [
'title' => 'title',
'description' => $this->stringValue
];
}
try {
// update video information
$this->service->videos->update(
'snippet,localizations',
$videoInfo
);
} catch (Exception $e) {
// do nothing and continue
}
return;
}
It seems that the language that you used (zh-Hant & zh-Hans) is not supported as of the moment.
To verify whether a language is supported or not, you may utilize i18nLanguages.list API. Here's a sample request using API explorer.
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.i18nLanguages.list?part=snippet&_h=1&
Related
I have Yii2-advanced project where I can download PDF. Everything works great on PC but on mobile devices download is creating .html extension like "example.pdf.html".
I tried to add header at the beginning of function but still have no success. I know that there must be header like
header("Content-type:application/pdf");
who tells what kind of file it will be, but don't know where is the problem and why it is still downloading wrong file name.
Here is example of code:
public function run()
{
header('Content-Type', 'application/pdf');
if (strpos($this->type, 'e') === 0) {
$model = new DataModel($this->id);
try {
if (Yii::$app->request->get('button') === Yii::t('frontend', 'Print')) {
$pdf = $model->pdf();
$pdf->Output("{$model->id}.pdf", 'D');
return null;
}
} catch (MpdfException $e) {
Yii::error($e->getMessage());
}
$this->type = self::TYPE_EXAMPLE;
return $this->render($this->type, ['model' => $model]);
}
$model_ids = explode(',', $this->id);
$model_ids = array_unique(array_filter(array_map('trim', $model_ids)));
$models = [];
foreach ($model_ids as $id) {
$models[] = new DataModel($id);
}
try {
if (Yii::$app->request->get('print') !== null) {
$model = new DataModel(Yii::$app->request->get('print'));
$pdf = $model->pdf();
$pdf->Output("{$model->id}.pdf", 'D');
return null;
}
if (Yii::$app->request->get('button') === Yii::t('frontend', 'Print all')) {
$pdf = DataModel::array_pdf($models);
$pdf->Output("{$this->id}.pdf", 'D');
return null;
}
} catch (MpdfException $e) {
Yii::error($e->getMessage());
}
$this->type = self::TYPE_EXAMPLE_2;
return $this->render($this->type, [
'models' => $models,
'ids' => $this->id]);
}
I know that if I am using "dest" D in pdf Output method it should set header, but still it don't work. Can anyone help me with this problem?
I am fairly new to Laravel. This may have an obvious solution but I can't seem to find it so far. Therefore I am asking for help.
Question In Short:
I use Illuminate\Http\Request session ($request->session()) to store some data I get from BigCommerce API. But I can't get them when I need the data.
Context:
I am building a sample app/boilerplate app for BigCommerce platform using Laravel/React. I have built the app using official documentation, semi-official posts released by BigCommerce team and sample codebase provided by them as well.
App works fine with local credentials from a specific store because they are given as environment variables to the app. However I can't read store_hash (which is necessary to fetch data from BigCommerce) and access token. Both I have put in $request->session() object.
I will paste the AppController.php code below, also code is publicly available here:
In the makeBigCommerceAPIRequest method below (as you can see my debugging efforts :)) I can get $this->getAppClientId(), but I can't get anything from $request->session()->get('store_hash') or $request->session()->get('access_token') which returns from $this->getAccessToken($request).
I have tried putting store hash into a global variable, but it didn't work.
From everything I have experienced so far, $request is not working as expected.
Any help appriciated, thanks in advance.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use mysql_xdevapi\Exception;
use Oseintow\Bigcommerce\Bigcommerce;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Bigcommerce\Api\Client as BigcommerceClient;
use Illuminate\Support\Facades\Storage;
use App\Config; //Database Connection
use Bigcommerce\Api\Connection;
class AppController extends Controller
{
protected $bigcommerce;
private $client_id;
private $client_secret;
private $access_token;
private $storehash;
private $redirect_uri;
public function __construct(Bigcommerce $bigcommerce)
{
$this->bigcommerce = $bigcommerce;
$this->client_id = \config('app.clientId');
$this->client_secret = \config('app.clientSecret');
$this->redirect_uri = \config('app.authCallback');
}
public function getAppClientId()
{
if (\config('app.appEnv') === 'local') {
return \config('app.localClientId');
} else {
return \config('app.clientId');
}
}
public function getAppSecret()
{
if (\config('app.appEnv') === 'local') {
return \config('app.localClientSecret');
} else {
return \config('app.clientSecret');
}
}
public function getAccessToken(Request $request)
{
if (\config('app.appEnv') === 'local') {
return \config('app.localAccessToken');
} else {
return $request->session()->get('access_token');
}
}
public function getStoreHash(Request $request)
{
if (\config('app.appEnv') === 'local') {
return \config('app.localStoreHash');
} else {
return $request->session()->get('store_hash');
}
}
public function error(Request $request)
{
$errorMessage = "Internal Application Error";
if ($request->session()->has('error_message')) {
$errorMessage = $request->session()->get('error_message');
}
echo '<h4>An issue has occurred:</h4> <p>' . $errorMessage . '</p> Go back to home';
}
public function load(Request $request)
{
$signedPayload = $request->get('signed_payload');
if (!empty($signedPayload)) {
echo "hello";
$verifiedSignedRequestData = $this->verifySignedRequest($signedPayload);
if ($verifiedSignedRequestData !== null) {
echo "positive return";
$request->session()->put('user_id', $verifiedSignedRequestData['user']['id']);
$request->session()->put('user_email', $verifiedSignedRequestData['user']['email']);
$request->session()->put('owner_id', $verifiedSignedRequestData['owner']['id']);
$request->session()->put('owner_email', $verifiedSignedRequestData['owner']['email']);
$request->session()->put('store_hash', $verifiedSignedRequestData['context']);
echo $request->session()->get('store_hash');
$this->storehash = $verifiedSignedRequestData['context'];
echo ' store hash is at the moment : ' . $this->storehash . ' .....';
} else {
return "The signed request from BigCommerce could not be validated.";
// return redirect()->action([AppController::class, 'error'])->with('error_message', 'The signed request from BigCommerce could not be validated.');
}
} else {
return "The signed request from BigCommerce was empty.";
// return redirect()->action([AppController::class, 'error'])->with('error_message', 'The signed request from BigCommerce was empty.');
}
return redirect(\config('app.appUrl'));
}
public function install(Request $request)
{
// Make sure all required query params have been passed
if (!$request->has('code') || !$request->has('scope') || !$request->has('context')) {
echo 'Not enough information was passed to install this app.';
// return redirect()->action('MainController#error')->with('error_message', 'Not enough information was passed to install this app.');
}
try {
$client = new Client();
$result = $client->request('POST', 'https://login.bigcommerce.com/oauth2/token', [
'json' => [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'redirect_uri' => $this->redirect_uri,
'grant_type' => 'authorization_code',
'code' => $request->input('code'),
'scope' => $request->input('scope'),
'context' => $request->input('context'),
]
]);
$statusCode = $result->getStatusCode();
$data = json_decode($result->getBody(), true);
if ($statusCode == 200) {
$request->session()->put('store_hash', $data['context']);
$request->session()->put('access_token', $data['access_token']);
$request->session()->put('user_id', $data['user']['id']);
$request->session()->put('user_email', $data['user']['email']);
// $configValue = Config::select('*')->where('storehash', $data['context'])->get()->toArray();
// if (count($configValue) != 0) {
// $id = $configValue[0]['id'];
// $configObj = Config::find($id);
// $configObj->access_token = $data['access_token'];
// $configObj->save();
// } else {
// $configObj = new Config;
// $configObj->email = $data['user']['email'];
// $configObj->storehash = $data['context'];
// $configObj->access_token = $data['access_token'];
// $configObj->save();
// }
// If the merchant installed the app via an external link, redirect back to the
// BC installation success page for this app
if ($request->has('external_install')) {
return redirect('https://login.bigcommerce.com/app/' . $this->getAppClientId() . '/install/succeeded');
}
}
return redirect(\config('app.appUrl'));
} catch (RequestException $e) {
$statusCode = $e->getResponse()->getStatusCode();
echo $statusCode;
$errorMessage = "An error occurred.";
if ($e->hasResponse()) {
if ($statusCode != 500) {
echo "some error other than 500";
// $errorMessage = Psr7\str($e->getResponse());
}
}
// If the merchant installed the app via an external link, redirect back to the
// BC installation failure page for this app
if ($request->has('external_install')) {
return redirect('https://login.bigcommerce.com/app/' . $this->getAppClientId() . '/install/failed');
} else {
echo "fail";
// return redirect()->action('MainController#error')->with('error_message', $errorMessage);
}
}
// return view('index');
}
public function verifySignedRequest($signedRequest)
{
list($encodedData, $encodedSignature) = explode('.', $signedRequest, 2);
// decode the data
$signature = base64_decode($encodedSignature);
$jsonStr = base64_decode($encodedData);
echo $jsonStr;
$data = json_decode($jsonStr, true);
// confirm the signature
$expectedSignature = hash_hmac('sha256', $jsonStr, $this->client_secret, $raw = false);
if (!hash_equals($expectedSignature, $signature)) {
error_log('Bad signed request from BigCommerce!');
return null;
}
return $data;
}
public function makeBigCommerceAPIRequest(Request $request, $endpoint)
{
echo ' ...... trying to make an apiRequest now : with storehash : ' . $this->storehash . ' .............';
echo '...........................................';
echo 'other variables at the moment :::: ............... client ID :' . $this->getAppClientId() . '...................... token : ' . $this->getAccessToken($request) . '...............';
$requestConfig = [
'headers' => [
'X-Auth-Client' => $this->getAppClientId(),
'X-Auth-Token' => $this->getAccessToken($request),
'Content-Type' => 'application/json',
]
];
if ($request->method() === 'PUT') {
$requestConfig['body'] = $request->getContent();
}
$client = new Client();
$result = $client->request($request->method(), 'https://api.bigcommerce.com/' . $this->storehash . '/' . $endpoint, $requestConfig);
return $result;
}
public function proxyBigCommerceAPIRequest(Request $request, $endpoint)
{
if (strrpos($endpoint, 'v2') !== false) {
// For v2 endpoints, add a .json to the end of each endpoint, to normalize against the v3 API standards
$endpoint .= '.json';
}
echo ' asadssada ...... trying to make an apiRequest now : with storehash : ' . $this->storehash . ' .............' . $request->session()->get('store_hash') . ' ............ ';
$result = $this->makeBigCommerceAPIRequest($request, $endpoint);
return response($result->getBody(), $result->getStatusCode())->header('Content-Type', 'application/json');
}
}
Thanks for the detailed info. Though it would help to annotate the debug lines with confirmation of what they output, I am making the assumption that you have narrowed the problem down to the session storage and retrieval lines.
$request->session()->put() and get() are the correct ways to access session.
I would therefore suggest investigating Session configuration: https://laravel.com/docs/8.x/session#configuration
If using file-based sessions, confirm that there are no permissions errors, perhaps. Alternatively try and different session storage mechanism.
I am making an app that translates a word from one language to English and gets information about it (e.g. definition, use in a sentence, synonyms, sound representation)
What my function does:
Searches for the translation in the database. If it is found, we return it.
If it is not found we translate a word using google translate, or Yandex translate API.
If translation is found we download it's sound representation, save the translation to the database and add additional information from other API's
We return a json response with all of the information.
Now my controllers method is really big and I can't find a cleaner way to go about it.
Any help is appreciated.
public function store(Request $request)
{
$translated = $request->get('translated');
$translation = $this->translation->findBy('translated', $translated)->first();
if ($translation) {
return Response::json(['translation' => $this->translation->with(['examples', 'definitions', 'synonyms', 'images'])->find($translation->id)], ResponseCode::HTTP_CREATED);
}
$data = $request->all();
$data['translation'] = $this->translate($translated);
if ($translated == $data['translation']) {
Log::info('Translation not found: ' . $data['translation']);
return $this->translationNotFound();
}
$downloader = new Downloader(new GoogleSpeechDownloader());
$filename = $downloader->download($data['translation']);
if ($filename) $data['sound_name'] = $filename;
$translation = $this->translation->create($data);
$this->createDefinition($translation);
$this->createExample($translation);
$this->createSynonym($translation);
return Response::json(['translation' => $this->translation->with(['examples', 'definitions', 'synonyms', 'images'])->find($translation->id)], ResponseCode::HTTP_CREATED);
}
private function translationNotFound()
{
return Response::json(['error' => 'Vertimas nerastas.'], ResponseCode::HTTP_NOT_FOUND);
}
private function createDefinition($translation)
{
$definition = new Definition();
$definer = new Definer(new DictionaryApiDefiner());
try {
$definition->definition = $definer->getDefinition($translation->translation);
$definition->approved = true;
$translation->definitions()->save($definition);
} catch (\Exception $e) {
Log::alert('Definition for word ' . $translation->translation . ' not found.');
}
}
private function createExample($translation)
{
$example = new Example();
$exampler = new ExampleCreator(new YourDictionaryGouteParserExampler());
try {
$example->example = $exampler->getExample($translation->translation);
$example->approved = true;
$translation->examples()->save($example);
} catch (\Exception $e) {
Log::alert('Example for word ' . $translation->translation . ' not found.');
}
}
private function createSynonym($translation)
{
$creator = new SynonymCreator(new BigHugeLabsSynonymCreator());
foreach ($creator->getSynonyms($translation->translation) as $s) {
$synonym = new Synonym();
$synonym->synonym = $s;
$synonym->approved = true;
$translation->synonyms()->save($synonym);
}
}
private function translate($translated)
{
$translator = new Translator(new GoogleTranslator());
try {
return $translator->translate($translated);
} catch (\Exception $e) {
Log::critical($e->getMessage());
}
$translator = new Translator(new YandexTranslator());
return $translator->translate($translated);
}
If you want cleaner code, just make a class for this job. Two classes for this two API's and in the controller make the check for the word, if not exist in the database, check in the other two API's, just split every action to method in the new two classes that you will make.
I am attempting to add logging for the envelope generated by a third party library. I am modifying the updateMetadataField() method below.
I am creating $client like so:
$client = new UpdateClient($UPDATE_END_POINT, $USER_AUTH_ARRAY);
I have tried both $this->client->__getLastRequest() and $this->__getLastRequest() with the same error as a result.
When the SoapClient is instantiated trace is set to true.
Error is
Fatal error: Call to undefined method UpdateClient::__getLastRequest()
So how do I correctly access the __getLastRequest() method?
$USER_AUTH_ARRAY = array(
'login'=>"foo",
'password'=>"bar",
'exceptions'=>0,
'trace'=>true,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS
);
class UpdateClient {
private $client;
public function __construct($endpoint, $auth_array) {
$this->client = new SoapClient($endpoint, $auth_array);
}
public function updateMetadataField($uuid, $key, $value) {
$result = $this->client->updateMetadataField(array(
'assetUuid' => $uuid,
'key' => $key,
'value' => $value)
);
if(is_soap_fault($result)) {
return $result;
}
return $result->return . "\n\n" . $this->client->__getLastRequest();
} // updateMetadataField()
} // UpdateClient
UPDATE - adding calling code This code iterates over an array which maps our data to the remote fields.
What I am hoping to do is begin storing the envelope we send to aid in debugging.
$client = new UpdateClient($UPDATE_END_POINT, $USER_AUTH_ARRAY);
foreach ($widen_to_nool_meta_map as $widen => $nool) { // array defined in widen.php
if ($nool != '') {
// handle exceptions
if ($nool == 'asset_created') { // validate as date - note that Widen pulls exif data so we don't need to pass this
if (!strtotime($sa->$nool)) {
continue;
}
} else if ($nool == 'people_in_photo' || $nool == 'allow_sublicensing' || $nool == 'allowed_use_pr_gallery') {
// we store as 0/1 but GUI at Widen wants Yes/No
$sa->$nool = ($sa->$nool == '1') ? 'Yes' : 'No';
} else if ($nool == 'credit_requirements') {
$sa->$nool = $sa->credit_requirements()->label;
}
$result = $client->updateMetadataField($sa->widen_id, $widen, $sa->$nool);
if(is_soap_fault($result)) {
$sync_result = $sync_result . "\n" . $result->getMessage();
} else {
$sync_result = $sync_result . "\n" . print_r($result, 1);
}
} // nool field set
} // foreach mapped field
If you want to access UpdateClient::__getLastRequest() you have to expose that method on the UpdateClient class since the $client is a private variable. The correct way of calling it is $this->client->__getLastRequest().
Take a look at this working example, as you can see I'm consuming a free web service for testing purposes.
<?php
$USER_AUTH_ARRAY = array(
'exceptions'=>0,
'trace'=>true,
'features' => SOAP_SINGLE_ELEMENT_ARRAYS
);
class TestClient {
private $client;
public function __construct($endpoint, $auth_array) {
$this->client = new SoapClient($endpoint, $auth_array);
}
public function CelsiusToFahrenheit( $celsius ) {
$result = $this->client->CelsiusToFahrenheit(array(
'Celsius' => $celsius
)
);
if(is_soap_fault($result)) {
return $result;
}
return $result;
}
public function __getLastRequest() {
return $this->client->__getLastRequest();
}
}
try
{
$test = new TestClient( "http://www.w3schools.com/webservices/tempconvert.asmx?wsdl", $USER_AUTH_ARRAY);
echo "<pre>";
var_dump($test->CelsiusToFahrenheit( 0 ));
var_dump($test->__getLastRequest());
var_dump($test->CelsiusToFahrenheit( 20 ));
var_dump($test->__getLastRequest());
echo "</pre>";
}
catch (SoapFault $fault)
{
echo $fault->faultcode;
}
?>
I would like to show a dynamic progressbar in my applciation while uploading a video (*.flv format). I searched on the Web for more than 2 hours but I can't find any tutorial to guide me through this process.
What I have so far:
script that uploads a video
jQuery library included in the section
But what to do next? Here is the controller action for uploading a video I use:
public function uploadPublicVideoAction()
{
$request = $this->getRequest();
$media = $this->_getTable('Media');
$form = $this->_getForm('UploadPublicVideo',
$this->_helper->url('upload-public-video'));
// if POST data has been submitted
if ($request->isPost()) {
// if the UploadPublicVideo form has been submitted and the submitted data is valid
if (isset($_POST['upload_public_video']) && $form->isValid($_POST)) {
$data = $form->getValues();
$data['user_id'] = $this->view->identity->id;
$ext = end(explode('.', $form->video->getFileName()));
$dbTrans = false;
try {
$db = $this->_getDb();
$dbTrans = $db->beginTransaction();
$data['type'] = 'video';
$data['status'] = 'public';
$paths = $media->add($data, $ext);
if (file_exists($paths[0])) {
unlink($paths[0]);
}
if (file_exists($paths[1])) {
unlink($paths[1]);
}
// add filter for renaming the uploaded photo
$form->video->addFilter('Rename',
array('target' => $paths[0],
'overwrite' => true));
// upload the video
$form->video->receive();
// create a thumbnail
//$this->_helper->FlvThumbnail($path[0], $path[1]);
$db->commit();
$form->reset();
$this->view->success = 'Public video successfully uploaded';
} catch (Exception $e) {
if (true === $dbTrans) {
$db->rollBack();
}
$this->view->error = $e->getMessage();
}
}
}
$this->view->headTitle('Upload Public Video');
$this->view->form = $form;
}
Can anyone show me a simple way to use Zend_Progressbar and jQuery together to achieve a dynamic upload progressbar?
You can do either long (comet) or short polling (ajax) to achieve the desired effect. With the former I would suggest making the request in an iFrame and having your code write out JS which will get executed as they come in, with the latter just do something like:
var pollingId = window.setInterval(poll, 250);
function poll(){
//make an AJAX request, do something with it (like update your progress bar).
if(done){
window.clearInterval(pollingId);
}
}