Video upload with Graph API from user's desktop - api

i am trying to upload a local video to Facebook through the Graph API.
Here is an example from Facebook:
http://developers.facebook.com/blog/post/493/
That works nice, and i get an JSON response after submitting the from.
How can I get the respone ID, for use in, e.g. Javascript or directly as an GET-parameter on my server?
I don't want to upload the video on an public accessable server and then redirect the video upload to Facebook by using curl or something like that.
Has anyone an idea or an example of how i can get this to work?
Edit:
Here is the example i used:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_POST_LOGIN_URL";
$video_title = "YOUR_VIDEO_TITLE";
$video_desc = "YOUR_VIDEO_DESCRIPTION";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$post_url = "https://graph-video.facebook.com/me/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $access_token;
echo '<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">';
echo 'Please choose a file:';
echo '<input name="file" type="file">';
echo '<input type="submit" value="Upload" />';
echo '</form>';
?>
Taken from this page: http://developers.facebook.com/blog/post/493/
This is the whole response i get from the form (JSON response):
{
"id": "xxxxx19208xxxxx"
}
The problem is that the JSON response is inline in the page.
I thought to set a target in the form to an inline iframe but then i will not be able to access the JSON code.

Related

How to retrieve log from graylog over API

How can I search logs from a graylog server with PHP?
Assume the graylog servers is https://host.td/api/search/universal/absolute
This solution is implemented in PHP:
$url = 'https://host.td/api/search/universal/absolute'
. '?query=' . urlencode('field:value') //query which you would also perform on UI
. '&from=' . urlencode(Carbon::createFromTimestamp(0)) // min timestamp so we get all logs
. '&to=' . urlencode(Carbon::createFromTimestamp(NumberUtils::MAX_32_BIT_INT)) // max timestamp so we get all logs
. '&limit=' . $this->limit //how many results do we want?
. '&fields=' . urlencode('field1,field2,field3') //which fields do we want?
. '&filter=' . urlencode('streams:<stream_id>') //OPTIONAL: only search in this stream
. '&sort=' . urlencode('field:desc') //sort result
. '&decorate=false'; //decorate parameter
$res = (new Client())->get($url, [
// generate a token on graylog UI;
// we use basic auth, username=the token; password: hard coded string 'token'
'auth' => ['<token_value>', 'token'],
'headers' => ['Accept' => 'application/json'] //we want a json result
]);
$json = \GuzzleHttp\json_decode($res->getBody());
If you want to sort by a timestamp you provided, don't call it timestamp since in this case graylog's timestamp is used, not yours. I ended up using a prefix on every field I am storing.

module overriding in zen cart

I am new to Zen Cart and currently working on a project which has custom template. I am now stuck on override of module file new_products.php !! how to override new_products.php ? I want to change HTML in it. how can I do it ? I have made changes in new_products.php itself. I know it is not right way to do it, but I am confused where do I make changes/override module file ?
Here is the code where I made changes in main new_products.php file
while (!$new_products->EOF) {
$products_price = zen_get_products_display_price($new_products->fields['products_id']);
if (!isset($productsInCategory[$new_products->fields['products_id']])) $productsInCategory[$new_products->fields['products_id']] = zen_get_generated_category_path_rev($new_products->fields['master_categories_id']);
$list_box_contents[$row][$col] = array('params' => 'class="centerBoxContentsNew centeredContent back"' . ' ' . 'style="width:' . $col_width . '%;"',
'text' => (($new_products->fields['products_image'] == '' and PRODUCTS_IMAGE_NO_IMAGE_STATUS == 0) ? '' : '<center><h2>'.$new_products->fields['products_name'].'</h2>' . zen_image(DIR_WS_IMAGES . $new_products->fields['products_image'], $new_products->fields['products_name'], IMAGE_PRODUCT_NEW_WIDTH, IMAGE_PRODUCT_NEW_HEIGHT) . '</center>') . '<h3>' . $products_price.'</h3><p>'.$new_products->fields['products_description'].'</p>
<div class="button black"><a href="' . zen_href_link(zen_get_info_page($new_products->fields['products_id']), 'cPath=' . $productsInCategory[$new_products->fields['products_id']] . '&products_id=' . $new_products->fields['products_id']) . '">Add To Cart' .'</div>');
$col ++;
if ($col > (SHOW_PRODUCT_INFO_COLUMNS_NEW_PRODUCTS - 1)) {
$col = 0;
$row ++;
}
$new_products->MoveNextRandom();}
It's perfectly fine to modify new_products.php, but first copy
includes/modules/new_products.php
to
includes/modules/YOUR_TEMPLATE/new_products.php
(where YOUR_TEMPLATE is the name of your custom template).
Then make changes in the file
includes/modules/YOUR_TEMPLATE/new_products.php
You can determine your template name by going to admin->tools->template selection.
If you don't know how to create a custom template in Zen Cart, read this:
http://www.zen-cart.com/content.php?180

Display images from protected folder

I have a protected folder within Yii and I'm looking to display some of those images within the site. I've tried the following code and it works within the site/index controller in that it returns just the image I wanted.
However when I tried to separate the code it didn't work. Any help is appreciated.
Model
public function getImage() // will take file identifier as #param
{
$imageID = '2562584569'; // will eventually be dynamically assigned
$image = Images::model()->find('tmp_name=:id', array('id' => $imageID));
$dest = Yii::getPathOfAlias('application.uploads');
$file = $dest .'/' . $image->tmp_name . '.' . $image->extension;
if(file_exists($file)){
ob_clean();
header('Content-Type:' . $image->logo_type);
readfile($file);
exit;
}
}
And in the view
CHtml::link('<img src="' . Yii::app()->request->baseUrl .'/images/image" />', array('product/index', 'id'=>$data['product_id'], 'slug'=> $data['product_slug']));
Thanks
Jonnny
"protected" folder are not accessible from the client browser. This prevents people to have access to important files, like your source code.
If you want to store images inside "protected" and want them to be accessible, you need to publish them using CAssetManager.
Usage is something like:
$path = Yii::app()->basePath.'/path-inside-protected';
$yourImageUrl = Yii::app()->assetManager->publish($path);
Yii will then use the file as an asset, coping it to the "assets" folder, sibling to "protected". After that, you can just use the url returned on your HTML.
<img src="<?php echo $yourImageUrl ?>">
I went about it like this
CHtml::link('<img src="' . $this->createUrl('/images/image', array('data'=>$data['data'])) . '" />', array('product/index', 'id'=>$data['product_id'], 'slug'=> $data['product_slug']));
Model
public function actionImage($data)
{
$image = Images::model()->find('tmp_name=:data', array('id' => $data));
$dest = Yii::getPathOfAlias('application.uploads');
$file = $dest .'/' . $image->tmp_name . '.' . $image->extension;
if(file_exists($file)){
ob_clean();
header('Content-Type:' . $image->logo_type);
readfile($file);
exit;
}
}
Thanks for all help

How to assert the current URL?

I'm unittesting my Yii application with PHPUnit and Selenium-server.
Code:
class StartSurveyRedirectTest extends WebTestCase
{
public $fixtures=array(
'sessions'=>'SurveySession',
'surveys' => 'Survey',
);
public function testActive() {
$survey = $this->surveys('survey_active');
$user_id = 1;
$imid = 'nl1234-9876';
$this->open('veldwerk/survey/redirect?survey_id=' . $survey->survey_id .
'&user_id=' . $user_id .
'&imid=' . $imid .
'&' . $survey->security_parameter_name . '=' . $survey->getSecurityParameterValue()
);
//assert if redirected
$this->assertNotEquals($this->url(),TEST_BASE_URL .
'veldwerk/survey/redirect?survey_id=' . $survey->survey_id .
'&user_id=' . $user_id .
'&imid=' . $imid .
'&' . $survey->security_parameter_name . '=' . $survey->getSecurityParameterValue()
);
}
}
The result when I start the test:
There was 1 error:
1) StartSurveyRedirectTest::testActive
Method url not defined.
D:\workspace\Pi\framework\test\CWebTestCase.php:63
D:\workspace\Pi\protected\tests\functional\StartSurveyRedirectTest.php:27
D:\workspace\Pi\protected\tests\functional\StartSurveyRedirectTest.php:27
Caused by
BadMethodCallException: Method url not defined.
D:\workspace\Pi\framework\test\CWebTestCase.php:63
D:\workspace\Pi\protected\tests\functional\StartSurveyRedirectTest.php:27
D:\workspace\Pi\protected\tests\functional\StartSurveyRedirectTest.php:27
So what I really want to test is if the user has been redirected. I thought the easiest way was to check the URL but it turns out it doesn't work? While in the selenium docs and PHPUnit docs says it can..
Does the Yii framework does not support this? Or am I doing something wrong?
Instead of using url as a method, try it as a property:
$this->url
I'm at work now and am able to check this out. I think the proper code for PHPUnit with Selenium is:
$driver->getCurrentPageUrl();
So it is a method from the driver instance.
Turns out PHPUnit_Extensions_SeleniumTestCase_Driver is an ancestor class of WebTestCase the Yii class I extend in my test.
PHPUnit_Extensions_SeleniumTestCase_Driver implements the method getLocation() which holds the current browser url. This allows me to use $this->getLocation()
SeleniumTestCase_Driver class reference
The result of my code:
//assert if redirected
$this->assertNotEquals($this->getLocation(),TEST_BASE_URL .
'veldwerk/survey/redirect?survey_id=' . $survey->survey_id .
'&user_id=' . $user_id .
'&imid=' . $imid .
'&' . $survey->security_parameter_name . '=' . $survey->getSecurityParameterValue()
);

INSERT in the query part of BigQuery, using google-api-php-client

I wanted to insert thousands of records in the database using INSERT command in php script , as it will easier to access, but https://developers.google.com/bigquery/docs/query-reference , this doesn't show the INSERT command that can be used while querying in php , like ,
$quert->setQuery("Insert into ... values...") , Have tried this in the Query Table of BigQuery web console, but it doesn't seem to work , Is there anyway to use setQuery() with some other command, to insert data ?
BigQuery doesn't support the INSERT command. You would need to create a load job. See https://developers.google.com/bigquery/docs/import#localimport for more information.
In addition to Jordan's answer, here's a snippet of code that should get you started using the Google BigQuery API and the Google API PHP client library for loading your own data into BigQuery programmatically. Note, this snippet simply spits out the raw API response of the load job, including the job Id to the screen - you'll have to add your own polling logic to check on the load job status.
(We will be adding additional documentation about loading your own data, as well as more PHP samples soon).
<?php
require_once "google-api-php-client/src/Google_Client.php";
require_once "google-api-php-client/src/contrib/Google_BigqueryService.php";
session_start();
$client = new Google_Client();
// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
$client->setScopes(array('https://www.googleapis.com/auth/bigquery'));
$client->setClientId('XXXXXXXXX.apps.googleusercontent.com');
$client->setClientSecret('XXXXXXXXX');
$client->setRedirectUri('http://YOURAPPLICATION/index.php');
// Instantiate a new BigQuery Client
$bigqueryService = new Google_BigqueryService($client);
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
?>
<!doctype html>
<html>
<head>
<title>BigQuery API Sample</title>
</head>
<body>
<div id='container'>
<div id='top'><h1>BigQuery API Sample</h1></div>
<div id='main'>
<?php
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
// Your project number, from the developers.google.com/console project you created
// when signing up for BigQuery
$project_number = 'XXXXXXXXXXXXXX';
// Information about the destination table
$destination_table = new Google_TableReference();
$destination_table->setProjectId($project_number);
$destination_table->setDatasetId('php_test');
$destination_table->setTableId('my_new_table');
// Information about the schema for your new table
$schema_fields = array();
$schema_fields[0] = new Google_TableFieldSchema();
$schema_fields[0]->setName('first');
$schema_fields[0]->setType('string');
$schema_fields[1] = new Google_TableFieldSchema();
$schema_fields[1]->setName('last');
$schema_fields[1]->setType('string');
$destination_table_schema = new Google_TableSchema();
$destination_table_schema->setFields($schema_fields);
// Set the load configuration, including source file(s) and schema
$load_configuration = new Google_JobConfigurationLoad();
$load_configuration->setSourceUris(array('gs://YOUR_GOOGLE_CLOUD_STORAGE_BUCKET/file.csv'));
$load_configuration->setDestinationTable($destination_table);
$load_configuration->setSchema($destination_table_schema);
$job_configuration = new Google_JobConfiguration();
$job_configuration->setLoad($load_configuration);
$load_job = new Google_Job();
$load_job->setKind('load');
$load_job->setConfiguration($job_configuration);
$jobs = $bigqueryService->jobs;
$response = $jobs->insert($project_number, $load_job);
echo '<pre>';
print_r($response);
echo '</pre>';
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Authorize Access to the BigQuery API</a>";
}
?>
</div>
</div>
</body>
</html>