Generate thumbnail in php, posting to Azure Computer Vision API - php

I want to use Azure Computer Vision API to generate thumbnails for my Wordpress site. I'm trying to make it work in php with wp_remote_post, but i don't know how to parse the parameters ? It returns a thumbnail in really bad quality and default 500x500px. Any ideas on how to resolve this issue ?
function get_thumbnail($URL) //* * * * Azure Computer Vision API - v1.0 * * * *
{
$posturl='https://api.projectoxford.ai/vision/v1.0/generateThumbnail';
$request = wp_remote_post($posturl, array(
'headers' => array(
'Content-Type' => 'application/json',
'Ocp-Apim-Subscription-Key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'),
'body' => array('url' => $URL)
));
if ( is_wp_error( $request ) )
{
$error_message = $request->get_error_message();
return "Something went wrong: $error_message";
} else
{
return $request['body'];
}
}
EDIT 1
Thanks #Gary your right! Now the cropping is correct, but i got a huge problem with the quality! I'm using a trial but i see no info from Azure on downgrading the thumb quality for trial users. They are claiming to deliver high quality thumbnails, but if thats the standard it's totaly useless.
I must have overlooked something i guess?
Of course Gary, if i get no correct answer on my quality question i will close the thread with your answer as correct.

According the description of Get Thumbnail, the width,height and smartCropping should be set as request parameters which should combined in URL.
However the second args in wp_remote_post() do not accept the URL parameters and will do nothing on them. So you need to combine the url first before set into wp_remote_post().
You can try to use add_query_arg() to combine your url first,
$posturl='https://api.projectoxford.ai/vision/v1.0/generateThumbnail';
$posturl=add_query_arg( array(
'width' => 600,
'height' => 400,
'smartCropping' => true
), $posturl);

Related

Uploaded video via DailyMotion API is not watchable by other users

I am using PHP SDK to upload videos to my DailyMotion channel, but i don't understand how this work.
I do this:
// Temporarily upload a file on Dailymotion' servers
// This does not create a video, it only offers you a public URL to work with.
$url = $api->uploadFile($videoPath);
try {
// More fields may be mandatory in order to create a video.
// Please refer to the complete API reference for a list of all the required data.
$result = $api->post(
'/me/videos',
array(
'url' => $url,
'title' => $title,
'tags' => 'video,publish,api',
'published' => true,
'is_created_for_kids' => false
)
);
print_r($result);
} catch(\Exception $e) {
die('exception: ' . $e->getMessage());
}
$api->logout();
When the video is uploaded, i keep getting a message saying "Publication in progress" for days (a week now and still getting the same message).
Although i can see/watch the video just fine if i used the account it belongs to.
I'm using 'GRANT_TYPE_PASSWORD' to upload the videos to my account.
I don't want my videos to be published in search results though, i just want my app users to be able to watch them embeded.
Any help would be highly appreciated, thanks in Advance.

Active Collab API: How to get projects

I'm trying out the ActiveCollab API for my first time. I had to use StackOveflow to figure out how to get the API token since the docs don't tell me this.
Below is my code:
/* GET INTENT */
$url = 'https://my.activecollab.com/api/v1/external/login';
$fields = array(
'email' => "email#email.com",
'password' => "****"
);
$intent = curl_post_connector($url, $fields);
$intent = $intent->user->intent;
/* GET TOKEN */
$url = 'https://app.activecollab.com/my_app_id/api/v1/issue-token-intent';
$fields = array(
'intent' => $intent,
'client_name' => 'My App Name',
'client_vendor' => 'My Company Name'
);
$token = curl_post_connector($url, $fields);
$token = $token->token;
Everything above works and get's the token properly. What I find really weird is that I have to use API v1 to get this, and the docs on ActiveCollab's site don't mention any URL for API v5. It seems like this is the approach everything is taking here on StackOverflow.
Now with the token, I try to get my list of projects:
/* GET PROJECT */
$url = 'https://app.activecollab.com/my_app_id/api/v1/users';
$headers = array (
"X-Angie-AuthApiToken" => $token
);
$projects = curl_get_connector($url, $headers);
var_dump($projects);
But this does not work. There is no error returned - it instead returns an array of languages for some reason! I don't want to paste the massive json object here, so instead I'll link you to a photo of it: https://www.screencast.com/t/7p5JuFB4Gu
UPDATE:
When attempting to use the SDK, it works up until I try getting the token (which is just as far as I got without the SDK). I'm getting Server Error 500, and when looking at the logs, it says:
/home/working/public_html/ac/index.php(21): ActiveCollab\SDK\Authenticator\Cloud->issueToken(123456789)
#1 {main}
thrown in /home/working/public_html/ac/SDK/Authenticator/Cloud.php on line 115
This is line 115 of Cloud.php:
throw new InvalidArgumentException("Account #{$account_id} not loaded");
I honestly don't think I did anything wrong... there must be something wrong with my account ID.
Just for kicks, I commented out that line, and the error disappears and the page loads fine - except now I have no token...

LARAVEL - Response from SAME url, showing different results

UPDATE 1
#lukepolo found out that the number 23842515550750742 randomly always goes to 23842515550750744 in JS. Any ideas why?
I am using Laravel for an API I am creating. I am using Forge to run the servers.
When I pull a URL for the interface in Chrome, Safari, FireFox, etc., it is returning an ID that has been increased by 2. When I pull that SAME url, with the same exact request in Postman, CRUD, Terminal via cURL, etc., it returns the CORRECT response.
Results from Postman:
accountid: 1334372826597482
campaign:"Facebook"
campaignid: 23842515550750742 <-- CORRECT
client_id: 72
id: 817
Results from Chrome:
accountid:1334372826597482
campaign:"Facebook"
campaignid:23842515550750744 <-- INCORRECT
client_id:72
id:817
I am using the Facebook Marketing API to save the information locally, so I can run massive queries in a much faster resposne than their API would give me. Here is the code for listing the campaigns with basic information:
$this->parseRequestData($request);
$campaigns = FacebookPerformanceOverview::whereIn('client_id', $this->sheet_ids)
->whereIn('accountid', $this->accountIds['facebook'])
->whereIn('campaignid', $this->campaigns)
->whereBetween('date', [$this->start_date, $this->end_date])
->groupBy('campaignid')
->get();
$response = [];
foreach ($campaigns AS $campaign) {
$data = FacebookPerformanceOverview::whereIn('client_id', $this->sheet_ids)
->whereIn('accountid', $this->accountIds['facebook'])
->where('campaignid', $campaign->campaignid)
->whereIn('adsetid', $this->adsets)
->whereBetween('date', [$this->start_date, $this->end_date]);
$response[] = array(
'company' => $campaign->company,
'campaign' => FacebookCampaignSettings::where('campaignid', $campaign->campaignid)->first(),
'impressions' => $data->sum('impressions'),
'clicks' => $data->sum('clicks'),
'cost' => $data->sum('cost'),
'reach' => $data->sum('reach'),
'frequency' => $data->avg('frequency'),
'uniqueclicks' => $data->sum('uniqueclicks'),
'actions' => $data->sum('actions'),
'offsiteconversions' => $data->sum('offsiteconversions'),
'leads' => $data->sum('actionsleadgen') + $this->calculateLeads(),
);
}
return response()->json($response, 200, [], JSON_NUMERIC_CHECK);
I am unable to give the URL, due to it containing sensitive information, but I am confused as to what is going on. I have cleared cache, different browsers, different computers, etc. Web browsers always return the ID in the network tab incorrectly. Any ideas?
Javascript not parsing large number in JSON correctly
Is the answer.

Google Analytics server-side tracking

Google Analytics, by just placing its sourcecode on my website, automatically tracks everything I used to need (pageviews, unique visitors).
But now, I need to track events, and the only way to do this is to do it server-side. Each time any users does an specific action i need to track, the server posts data to google to track the information, as explained here:
https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#event
And it does works amazingly perfect, but, since I realiced, I am now receiving a LOT of visits from Spain, doubling the visits from USA. And before I implemented the event tracking, Spain wasn't even part of the top 10 countries.
Today I have realiced that my servers are in Spain, and that may be causing the issue.
How can I track the event, without making it count as a pageview?
$url = 'http://www.google-analytics.com/collect';
$data = array('v' => '1', 'tid' => 'UA-HIDDEN-1', 'cid' => $_SERVER["REMOTE_ADDR"], 'ni' => '1', 't' => 'event', 'ec' => '', 'ea' => 'JUMP', 'el' => '');
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
Thank you very much!!
You are sending the IP adress as a client id, which is wrong. For one, the client id is supposed to be an UUID. Secondly, Analytics won't recognize that these events belong to an existing user.
You'd need to grab the existing client id for an existing user on the web page:
ga(function(tracker) {
var clientId = tracker.get('clientId');
});
and then send it back to the server and use it in your request (1). At the moment GA cannot assign correct geo information since the events do not belong to the session of the user who initiates the event (this quite possibly affects some other metrics, too).
(1) You might as well read the GA cookie in PHP, but Google recommends against it since the cookie format might change without notice. The script above will always return a correct client id even if the cookie format changes.
Updated: I have read a bit more documentation and while my answer seems still somewhat relevant it's probably wrong for the actual use case - Geo is determined by IP and the serverside script will still send the servers IP. So quite possibly (haven't done the science yet) this would look like one visitor with two devices instead of a single visitor.
Update 2: Apparently it is now possible to include the users IP adress as parameter, so this answer is possibly no longer relevant.
Here is a techopad presentation about mixing UA client- and serverside, maybe that helps.
An event in and of itself is not a pageview. See: Event Tracking
Is there a specific reason why you need to track events server side and pageviews from the normal ga.js client-side code?
You can easily track events from the client side, if you were unaware of that:
Click Link to Track Event
Assuming that you needed to keep events AND pageviews on the server side:
<?php
//Put SERVER_ADDR into a var
$request_ip = $_SERVER['REMOTE_ADDR'];
// Put any server IPs you need to filter out below in an array
$localhosts = array('127.0.0.1','192.168.15.1','10.1.10.1');
// Use this later
$url = 'http://www.google-analytics.com/collect';
Now, Figure out what to do with the REMOTE_ADDR check if its in our list above. then build an array of type to send GA (events, pageviews)
$actions = array();
// Note that the values are arbitrary and will let you do what you need.
if(in_array($request_ip)){
//Only track event, or track pageview differently, or track two events.
$handle_myServer = true;
$actions = ('event');
} else {
// Track everyone else
$handle_myServer = false;
$actions = ('event','pageview','mySpecialPageview','mySpecialEvent');
}
Finally We have built a list of events we can use in flow control with existing code for pageviews, user timing, events, etc. Be creative!
foreach($actions as $action){
$data = null; $options=null;
if($handle_myServer){
$someFlagForGA = 'RequestFromSpainServer';
}
if($action == 'event'){
$data = array('v' => '1'
, 'tid' => 'UA-HIDDEN-1',
,'cid' => $request_ip
,'ni' => '1'
, 't' => 'event'
, 'ec' => $someFlagForGA,
,'ea' => 'JUMP', 'el' => ''
);
} elseif($action == 'pageview'){
$data = array('v' => '1', 'tid' => 'UA-HIDDEN-1'
, 't' => 'pageview'
, 'dh'=> 'yourGAenabledDomainHere.com'
, 'dp'=> 'ViewedPage.html'
, 'dt'=> 'homepage'.' SERVER VISITED '.$someFlagForGA
);
} else {
// Do whatever else
}
// Would be better to do below with a single function
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
) ,$data);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context) or die('Error!!');
}
?>

Twitter API upload with PHP OAuth

I've been trying to find the best OAuth PHP lib for Twitter for hours. the TmhOAuth by Matt Harris seems kinda bloated (no offence) and started shooting PHP Warnings and Strict Standards notices right after I "installed" it.
All I want is to update my bg photo through the API. Just mine, so there is no need for any login and callbacks of any kind, all the keys are hard-coded.
In the end I found out about PHP's own thing: http://php.net/manual/en/book.oauth.php
Seemed cool because everything has 4 lines of code. The auth works and I can push stuff through the API, I just can't seem to send the image parameter though. This is the method: https://dev.twitter.com/docs/api/1/post/account/update_profile_background_image
I've found some examples that were using upload forms, but I have the photo already saved in a file, so how do I provide the, quote, base64-encoded image as raw multipart data?
$oauth->fetch
(
'https://api.twitter.com/1/account/update_profile_background_image.json',
array
(
'image' => '#' . $img_path . ';type=image/jpeg'
),
'POST'
);
Doesn't work, instead I get
Fatal error: Uncaught exception 'OAuthException' with message 'Invalid auth/bad request (got a 500, expected HTTP/1.1 20X or a redirect)'
You can encode the image like this:
<?php
$im = imagecreatefromjpeg('file.jpg');
$im_Data = base64_encode($im);
?>
Then you should be able to add $img_Data into the api call.
I don't know if it is an open issue yet but the "image" key must start with "#" too.
$oauth->fetch
(
'https://api.twitter.com/1/account/update_profile_background_image.json',
array
(
'#image' => '#' . $img_path . ';type=image/jpeg'
),
'POST'
);
In case anyone is interested I ended up using TmhOAuth in the end. Bloated but it did the job.
$image = array
(
"#$path",
'type=image/jpeg',
"filename=$name"
);
$params = array
(
'image' => implode(';', $image),
'use' => 'true'
);
// Request
$code = $tmhOAuth->request('POST', $tmhOAuth->url
(
'1/account/update_profile_background_image'
),
$params, true, true);

Categories