Upload video to jw player account using Laravel - php

I want to add video file or video URL to jwplayer in my laravel 5.2 project but i didn't found it's any package or library yet. It's documentation is very difficult to understand for me as I never worked on jwplayer before. My requirement is to upload video to jwplayer and amazon s3 bucket. It's uploading to s3 bucket but I'm stuck at jwplayer. Any guide or example will be helpful.

Use this code...
<?php
require_once('vendor/autoload.php');
function get_parts_count($file) {
// 5 MB is the minimum size of an upload part, per https://developer.jwplayer.com/jwplayer/docs/stream-multipart-resumable-upload
$minimum_part_size = 5 * 1024 * 1024;
$size = filesize($file);
return (floor($size / $minimum_part_size) + 1);
}
$secret = $_ENV('JWPLATFORM_API_SECRET');
$site_id = $_ENV('JWPLATFORM_SITE_ID');
$jwplatform_api = new Jwplayer\JwplatformClient($secret);
$target_file = 'examples/test.mp4';
$params = array();
$params['metadata'] = array();
$params['metadata']['title'] = 'PHP API Test Upload';
$params['metadata']['description'] = 'Video description here';
$params['upload'] = array();
$params['upload']['method'] = 'multipart';
$create_response = json_encode($jwplatform_api->Media->create($site_id, $params));
print_r($create_response);
print("\n");
$decoded = json_decode(json_decode(trim($create_response), true), true);
$upload_id = $decoded['upload_id'];
$upload_token = $decoded['upload_token'];
$media_id = $decoded['id'];
$parts_count = get_parts_count($target_file);
$upload_parts_url = "https://api.jwplayer.com/v2/uploads/$upload_id/parts?page=1&page_length=$parts_count";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $upload_parts_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $upload_token,
),
));
$upload_parts_response = curl_exec($curl);
curl_close($curl);
$upload_parts_response_object = json_decode($upload_parts_response);
print_r($upload_parts_response_object);
print("\n");
$upload_url_parts = $upload_parts_response_object->parts;
// 5 MB is the minimum size of an upload part, per https://developer.jwplayer.com/jwplayer/docs/stream-multipart-resumable-upload
$buffer = (1024 * 1024) * 5;
if (count($upload_url_parts) > 0) {
$file_handle = fopen($target_file, 'rb');
foreach($upload_url_parts as $upload_url_parts_key => $upload_url_parts_value) {
$upload_bytes_url = $upload_url_parts_value -> upload_link;
$file_part = fread($file_handle, $buffer);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $upload_bytes_url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_POSTFIELDS, $file_part);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:'
));
$upload_bytes_response = curl_exec($curl);
print_r($upload_bytes_response);
print("\n");
}
fclose($file_handle);
$complete_upload_url = "https://api.jwplayer.com/v2/uploads/$upload_id/complete";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $complete_upload_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer ' . $upload_token,
),
));
$complete_upload_response = curl_exec($curl);
curl_close($curl);
$complete_upload_response_object = json_decode($complete_upload_response);
print_r($complete_upload_response_object);
print("\n");
}
?>

From what I can tell Laravel is a PHP framework and it shouldn't be a problem to use the JW Player APIs within that. Have you checked the documentation here:
https://developer.jwplayer.com/jw-platform/reference/v1/s3_uploads.html
There are also a couple of script examples (PHP and Python) that you can use here to get started:
https://developer.jwplayer.com/jw-platform/

There are multiple steps in uploading a video on jwplayer:-
$apiKey = env('jw_player_apikey');
$apiV2Secret = env('jw_player_secretkey);
// Step1. for getting upload_token and upload_id
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.jwplayer.com/v2/sites/$apiKey/media/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"upload\":{\"method\":\"multipart\"},\"metadata\":{\"title\":\"$r->title\",\"description\":\"My media description\"}}",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: $apiV2Secret",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return response()->json(['status'=>'error']);
} else {
$response=json_decode($response);
// Step2. For getting upload_link
$curl1 = curl_init();
curl_setopt_array($curl1, [
CURLOPT_URL => "https://api.jwplayer.com/v2/uploads/$response->upload_id/parts?page=1&page_length=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer ".$response->upload_token,
"Content-Type: application/json"
],
]);
$response1 = curl_exec($curl1);
$err1 = curl_error($curl1);
curl_close($curl1);
if ($err1) {
return response()->json(['status'=>'error']);
}
else {
$response1=json_decode($response1);
$url = $response1->parts[0]->upload_link;
// Step4. For uploading parts
$curl2 = curl_init($url);
curl_setopt($curl2, CURLOPT_URL, $url);
curl_setopt($curl2, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl2, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($curl2, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl2, CURLOPT_HTTPHEADER, array('Content-Type:'));
$file = $r->file('video');
$handle = fopen($file, "r");
$data = fread($handle, filesize($file));
curl_setopt($curl2, CURLOPT_POSTFIELDS, $data);
$resp = curl_exec($curl2);
$err2 = curl_error($curl2);
curl_close($curl2);
if ($err2) {
return response()->json(['status'=>'error']);
}
else {
// Step 5. to complete the upload
$curl3 = curl_init();
curl_setopt_array($curl3, [
CURLOPT_URL => "https://api.jwplayer.com/v2/uploads/".$response->upload_id."/complete",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer ".$response->upload_token,
"Content-Type: application/json"
],
]);
$response3 = curl_exec($curl3);
$err3 = curl_error($curl3);
curl_close($curl3);
if ($err3) {
return response()->json(['status'=>'error']);
} else {
// Step 6. Get uploaded video details
$curl6 = curl_init();
curl_setopt_array($curl6, [
CURLOPT_URL => "https://api.jwplayer.com/v2/sites/$apiKey/media/$response->id",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Authorization: Bearer $apiV2Secret",
"Content-Type: application/json"
],
]);
$response6 = curl_exec($curl6);
$err6 = curl_error($curl6);
curl_close($curl6);
if ($err6) {
return response()->json(['status'=>'error']);
} else {
$video_response=json_decode($response6);
//get video id from the response
if($this->check_count($video_response)>0){
//insert video detail in database
$insert=new videos();
$insert->jw_id=$video_response->id;
$insert->save();
if ($insert) {
return response()->json(['status'=>'success']);
} else {
return response()->json(['status'=>'error','message'=>"Fail to upload video, Please try again!!"]);
}
}
else{
return response()->json(['status'=>'error','message'=>"Fail to upload video, Please try again!!"]);
}
}
}
}
}
}
The above code doc was received from JW player official. It's working on my side. Hope it helps you too.

Related

Curl not executing in php on server

I have this function to send message
Code:
public static function send($mobile,$otp,$country_code){
$message = "Your 2-way authentication OTP is $otp";
$curl = curl_init();
$authKey = App::environment('MSGAUTH');
$senderId = App::environment('SENDERID');
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.XXX.com/api/sendhttp.php?authkey=".$authKey."&mobiles=".$mobile."&country=$country_code&sender=".$senderId."&route=3&message=$message",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTPHEADER => array(
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return false;
} else {
return true;
}
}
Issue is i'm getting blank quotes in the error part and this code is working on my local system but on server control is going in the else part. When i print $err i get this output
Any solution to resolve this issue.
Thanks

How to access the CURL to get the json data from php

How to access the CURL to get the json data from php.
By the way, I am using codeigniter framework.
This is curl data:
curl -u <YOUR_KEY_ID>:<YOUR_SECRET> \
-X POST https://api.razorpay.com/v1/orders \
-H "content-type: application/json" \
-d '{
"amount": 50000,
"currency": "INR",
"receipt": "receipt#1"
}'
and this my possible work to cosume it:
$url = 'https://api.razorpay.com/v1/orders';
$curl = curl_init($url);
$data = json_encode(array(
'receipt'=> $orderId,
'amount'=>$amount,
'currency'=> 'INR'
), JSON_FORCE_OBJECT);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json'
//$keyId.':'.$secretKey
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
Please help me to consume it!!
I am in trouble and also in hurry!!
If the response like :
[{"data1":"value1","data1.1":"value1.1"},{"data2":"value2","data2.1":"value2.1"}...]
Use :
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$json = file_get_contents('thelink', false, stream_context_create($arrContextOptions));
$result = json_decode($json, true);
and call it like :
echo $result[0]["data1"];
echo $result[0]["data1.1"];
echo $result[0]["data2"];
echo $result[0]["data2.1"];
If you still want to use curl (example on weatherAPI) :
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://weatherapi-com.p.rapidapi.com/forecast.json?q=London&days=1",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"x-rapidapi-host: weatherapi-com.p.rapidapi.com",
"x-rapidapi-key: aaaaxxxxbbbb"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$weather = json_decode($response);;
}
?>
Try this, I keep this for whenever I need to consume and API,
$url = 'https://api.razorpay.com/v1/orders';
$curl = curl_init();
$obj = new StdClass();
$obj->receipt = $orderId;
$obj->amount = $amount;
$obj->currency = 'INR';
$payload = json_encode($obj);
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $payload,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
//$keyId.':'.$secretKey
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
Get your response and handle any errors, maybe you are just missing a comma in your headers array

How to upload video using put.re api (using CURL)

I want to upload videos to put.re, a file hosting provider using php curl
I tried this code:
foreach ($_FILES['uploadvid']['tmp_name'] as $index => $fileTmpName) {
$fileName = $_FILES['uploadvid']['name'];
$size = $_FILES['uploadvid']['size'];
$handle = fopen($fileTmpName, "r");
$data = fread($handle, filesize($fileTmpName));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_URL => "https://api.put.re/upload",
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_TIMEOUT => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => array( 'file' => # $data),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$pms = json_decode($response, true);
$vidurl = $pms['data']['link'];
if ($vidurl!="") {
echo 'Success';
} else {
echo 'Problem';
echo $err;
echo $response;
}
}
But this echo Problem.
If you check the api docs, you will see there is no output for error.
You can check The Api Docs here . There is no example shown in it's site.
The $err returns nothing,
the $reponse returns a message: NO files(s) found.
I think there is a mistake in the API call...
Please Help me to get through this.
Please NOTE that, I want to upload videos, not images. put.re allows any kind of file to be uploaded. I tried to upload files less than 100mb (which is a limit)
Can you share more detail about the error you getting ? For example i tried the api with code at below and get response which says upload disabled.
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.put.re/upload",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"example.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Accept: */*",
"Accept-Encoding: gzip, deflate",
"Cache-Control: no-cache",
"Connection: keep-alive",
"Content-Length: 20895",
"Content-Type: application/x-www-form-urlencoded",
"Host: api.put.re",
"User-Agent: PostmanRuntime/7.20.1",
"cache-control: no-cache",
"content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}

Not Able to Get Data From Unocoin Public API

I am using cURL to get data from this link of Unocoing - https://www.unocoin.com/trade?all
function api_price($api_url){
if(!empty($api_url)){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $api_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
));
$response = curl_exec($curl);
$err = curl_error($curl);
return $response;
curl_close($curl);
}
}
if(!empty($_GET['url'])){
$api_url=$_GET['url'];
$tag=$_GET['tag'];
$api_price=api_price($api_url);
$response = json_decode($api_price, true);
$output= $response[$tag];
echo $output['lastPrice'];
}
I am using same code for other exchanges. But some like unocoin, coindelta show bool(false)
https://coindelta.com/api/v1/public/getticker/

ALM SaaS REST API 12.50 not working in PHP

I have tried below code in PHP to get the defect details from ALM but its not showing any response in browser. But the same is working in POSTMAN . Can someone help me here
Here is the document of REST API USAGE REST DOCUMENT FROM ALM
I have already tried existing posts from Stackoverflow
HP ALM REST API login using PHP CURL
ALM REST API v12.50 error 401
Nothing is helping so posted a new question
Note : Due to security purpose header value is kept as encoded value
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://hostname/qcbin/api/domains/domainname/projects/projectname/defects/?limit=10",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic encoded value",
"cache-control: no-cache",
"postman-token: a8a2398d-7a0a-0ebd-a586-58a40e524a9a"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
I have finally found the solution and below is the approach
First we need get to LWSSO_COOKIE_KEY,QCSession,ALM_USER,XSRF_TOKEN values from the ALM Authentication link then we should use the values for subsequent calls
Below is the complete working code to get the list of defects by entering ALM Credentials
<?php
$curl = curl_init();
Header('Content-type: application/json');
$credentials = "username:password";
curl_setopt_array($curl, array(
CURLOPT_URL => "https://host:port/qcbin/api/authentication/sign-in",
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic " . base64_encode($credentials) ,
"cache-control: no-cache"
) ,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
// If there is no error then get the response to form the array of headers to get the different values required
$array_start = explode(';', $response);
foreach ($array_start as $key => $value) {
$remove_from_string = ['HTTP/1.1 200 OK','Path=/','HTTPOnly','HttpOnly','Content-Length',': 0'];
$replace_array = ['','','','','',''];
$value = str_replace($remove_from_string,$replace_array,$value);
$value = trim(preg_replace(('/Expires: [a-zA-Z]+, [0-9]+ [a-zA-Z]+ [0-9]+ [0-9]+:[0-9]+:[0-9]+ [a-zA-Z]+/'), '', $value));
$value = trim(preg_replace(('/Server: [a-zA-Z0-9.\(\)]+/'),'',$value));
if (!empty($value)) {
$almheaders[trim(explode('=',$value)[0])] = explode('=',$value)[1];
}
}
$LWSSO_COOKIE_KEY = $almheaders['Set-Cookie: LWSSO_COOKIE_KEY'];
$QCSession = $almheaders['Set-Cookie: QCSession'];
$ALM_USER = $almheaders['Set-Cookie: ALM_USER'];
$XSRF_TOKEN = $almheaders['Set-Cookie: XSRF-TOKEN'];
// Now form the Cookie value from the above values.
$cookie = "Cookie: JSESSIONID=33eyr1y736486zcnl0vtmo12;XSRF-TOKEN=$XSRF_TOKEN;QCSession=$QCSession;ALM_USER=$ALM_USER;LWSSO_COOKIE_KEY=$LWSSO_COOKIE_KEY";
// echo $cookie;
$curl = curl_init();
Header('Content-type: application/json');
curl_setopt_array($curl, array(
CURLOPT_URL => "https://host:port/qcbin/api/domains/CET_NTD/projects/BILLING_OPERATIONS/defects",
// CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Basic " . base64_encode($credentials) ,
"cache-control: no-cache",
"Accept: application/json",
$cookie
) ,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
echo $response;
}
}
?>

Categories