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

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;
}

Related

Determine the file type downloaded by cURL

I have this code,
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://example.com/v1/items/id/preview",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer bearer-string",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
file_put_contents('file.jpeg', print_r($response, true));
}
From that code I can tell that the response is jpeg that's why I assigned file.jpeg as filename.
Now my problem, what if the response is not jpeg?
I could not find any function from https://www.php.net/manual/en/ref.filesystem.php that will dynamically determine what type of file is in response.
Any Idea about this.
You can use curl_getinfo to get content-type of what curl got
$mime = curl_getinfo(response , CURLINFO_CONTENT_TYPE);
and then simply check if $mime equals to 'image/jpeg'

Error creating natural language classifier with api using PHP, returns data too small error but works in Postman

The cURL request works in Postman with the following:
curl -i -u "apikey:12345" \
-F training_data=#rtcu.csv \
-F training_metadata="{\"language\":\"en\",\"name\":\"RTCU\"}" \
"https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers/v1/classifiers"
Postman's generated cURL code for PHP returns { "code" : 400, "error" : "Data too small", "description" : "The number of training entries received = 0, which is smaller than the required minimum of 5" }
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers",
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=\"training_data\"; filename=\"rtcu.csv\"\r\nContent-Type: text/csv\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"training_metadata\"\r\n\r\n{\"language\":\"en\",\"name\":\"RTCU\"}\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic 12345",
"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;
}
I've tried adding the "#" in front of the filename like suggested in other posts with no success. I haven't had this problem with other IBM Watson services and their cURL calls. What could be the issue?
Using CURLFile worked for me
$uploadFilePath = 'rtcu.csv';
$name = "RTCU";
$lang = "en";
$uploadFileMimeType = "text/csv";
$uploadFilePostKey = 'training_data';
$metaPostKey = "training_metadata";
$uploadFile = new CURLFile(
$uploadFilePath,
$uploadFileMimeType,
$uploadFilePostKey
);
$curlHandler = curl_init();
curl_setopt_array( $curlHandler, [
CURLOPT_URL => 'https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Basic 12345",
"cache-control: no-cache"
),
CURLOPT_POSTFIELDS => [
$uploadFilePostKey => $uploadFile,
$metaPostKey => "{\"language\":\"{$lang}\",\"name\":\"{$name}\"}"
],
] );
$response = curl_exec( $curlHandler );
$err = curl_error( curlHandler );
curl_close( curlHandler );
if ( $err ) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
}

Cannot recieve header information via PHP but with Postman [duplicate]

This question already has answers here:
Can PHP cURL retrieve response headers AND body in a single request?
(16 answers)
Closed 3 years ago.
I'm trying to get a Token in the header of an POST request. With Postman it works but when I run this in PHP I get the right content but cannot extract the header information.
I have copied the PHP code generated in Postman but I cannot extract the header information. I only get the content.
The code I used:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://xxxx.yyyy.xy/xxxxxx/xxxxxxxxxxx",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"Content-Type: application/x-www-form-urlencoded",
"Postman-Token: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
The header should include "Token".
I cannot recieve any header information.
I found a solution on the problem:
<?php
$curl = curl_init();
$Username = "XXXXXXXXX";
$Password = "YYYYYYYYYY";
$Credentials = base64_encode($Username.':'.$Password);
$Credentials_Auth = "Authorization: Basic ".$Credentials;
// echo "<br>Cred-auth: ".$Credentials_Auth."<br>";
curl_setopt_array($curl, array(
CURLOPT_URL => "https://XXXXXXXXX/YYYYYYYYYYY/ZZZZZZZZZZZ",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
**CURLOPT_HEADER => 1,**
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
$Credentials_Auth,
"Content-Type: application/x-www-form-urlencoded",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) { echo "cURL Error #:" . $err;}
else { echo $response; }
**$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headers = substr($response, 0, $header_size);
$body = substr($response, $header_size);**
curl_close($curl);
**header("Content-Type:text/plain; charset=UTF-8");
echo $headers;
echo $body;**
?>

IP long string filter

Hey,
i am trying to only show 1 bit of the long string i get from the api i got fram postman, the only thing i need to show is the city. How do i need do this?
i'm trying to find a way with php but i have no clue what to do
a:14:{s:10:"regionName";s:10:"California";s:6:"status";s:7:"success";s:4:"city";s:13:"Mountain View";s:8:"timezone";s:19:"America/Los_Angeles";s:7:"country";s:13:"United States";s:11:"countryCode";s:2:"US";s:3:"zip";s:0:"";s:3:"lon";d:-122.08499908447266;s:3:"isp";s:6:"Google";s:2:"as";s:19:"AS15169 Google Inc.";s:5:"query";s:7:"8.8.8.8";s:6:"region";s:2:"CA";s:3:"lat";d:37.42290115356445;s:3:"org";s:6:"Google";}
(im using the ip of google just for this question)
so the length of the city name changes!
the site where i got it frm http://ip-api.com/php/8.8.8.8
and the code i am using:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
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(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
That's a product of running a PHP variable in a PHP serialize you can reverse it with unserialize
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://ip-api.com/php/8.8.8.8",
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(
"cache-control: no-cache",
"postman-token: 2e83e542-a6fb-5bb6-94e0-c1908282a2a2"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
$responseArray = unserialize($response); //You probably need some error trapping here
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $responseArray["country"];
}

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