Does anyone know how to upload a video to Wistia using PHP cURL from a file upload input specified within a form? This is my code but I cannot seem to get this to work with the current API. I’ve looked at a few similar posts on this subject but still no luck...
<?php
$pathToFile = $_FILES['fileName']['tmp_name']; /* /tmp/filename.mov */
$nameOfFile = $_FILES['fileName']['name']; /* filename.mov */
$data = array(
'api_password' => '********',
'file' => '#'.$pathToFile,
'project_id' => '********'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, "https://upload.wistia.com" );
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
?>
UPDATE - FULL CODE (still not working)
cURL returns false for both curl_exec and curl_error when using "'file' => '#'.$pathToFile" but works fine when using "'url' = 'http://example.com/file.mov'... Perhaps I'm not processing the form properly? Here's the full code:
<?php
if ($_POST['submit']) {
$filePath = $_FILES['fileUploaded']['tmp_name'];
$fileName = $_FILES['fileUploaded']['name'];
$fileSize = $_FILES['fileUploaded']['size'];
echo("File Path: ");
echo $filePath;
echo '<br>';
$data = array(
'api_password' => '******',
/* 'url' => 'http://example.com/file.mov', WORKS */
'file' => '#'.$filePath, /* DOES NOT WORK */
'project_id' => '******'
);
// WORKING CMD LINE
// curl -i -F api_password=****** -F file=#file.mov https://upload.wistia.com/
/* cURL */
$chss = curl_init('https://upload.wistia.com');
curl_setopt_array($chss, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => http_build_query($data)
));
// Send the request
$KReresponse = curl_exec($chss);
$KError = curl_error($chss);
// Decode the response
$KReresponseData = json_decode($KReresponse, TRUE);
echo '<br>';
echo("Response:");
print_r($KReresponseData);
echo '<br>';
echo("Error:");
print_r($KError);
}
?>
<form name="upload-form" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<input type="file" name="fileUploaded">
<input type="submit" name="submit" value="Upload">
</form>
Any help would be much appreciated!
Try this:
$filePath = "#{$_FILES['fileUploaded']['tmp_name']};filename={$_FILES['fileUploaded']['name']};type={$_FILES['fileUploaded']['type']}";
Related
I don't know why this is not working. I am trying to download a image from a URL then sent it via CURL. POST request the path with var_dump is working, but with CURL it doesn't work
The error is
Warning: curl_file_create() expects parameter 1 to be a valid path, string given
// define('TMP', __DIR__ . DIRECTORY_SEPARATOR . 'tmp');
/**
* #param $url
* #return string
*/
function dlImage($url) {
$save_dir = TMP . DIRECTORY_SEPARATOR;
$filename = basename($url);
$save = $save_dir.$filename;
file_put_contents($save,file_get_contents($url));
return $save;
}
function upload( $body, $title, $type, $type_real, $subCat, $poster = null, $imdbUrl = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'xxxx');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
$data = [
'key' => 'xxxx',
'type' => $type,
'subtype' => $subCat,
'type_real' => $type_real,
'title' => $title,
'posterUpload' => curl_file_create(realpath(dlImage($poster)),'image/jpeg'),
'imdbUrl' => $imdbUrl,
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
}
// path is working fine
dlimage output (var_dump) : string(113)
"C:\laragon\www\api\tmp\MV5BNDliY2E1MjUtNzZkOS00MzJlLTgyOGEtZDg4MTI1NzZkMTBhXkEyXkFqcGdeQXVyNjMwMzc3MjE#.jpg"
add more error checking, replace
file_put_contents($save,file_get_contents($url));
with
$content=file_get_contents($url);
if(!is_string($content)){
throw new \RuntimeException("failed to fetch url {$url}");
}
if(($len=strlen($content))!==($written=file_put_contents($save,$content))){
throw new \RuntimeException("could only write {$written}/{$len} bytes to file {$save}");
}
then you should probably get an errorlog explaining where things went wrong, and my best guess is that things went wrong with file_get_contents() and/or file_put_contents().
i'm trying to use imgur as a uploading backend - i guess it secure my website if there's upload picture (is it right?) so
i went with this way :
<?php
$client_id = 'xxxxx';
$file = file_get_contents($_FILES["imgupload"]["tmp_name"]);
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
if ($error = curl_error($curl)) {
die('cURL error:'.$error);
}
$json_returned = curl_exec($curl); // blank response
echo "Result: " . $json_returned ;
curl_close ($curl);
?>
HTML form
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="imgupload" /><br>
<input type="submit" value="Upload to Imgur" />
</form>
when i click on submit the result is fine as i guess ^^
Result: {"data":{"id":"TvFtE29","title":null,"description":null,"datetime":1585015712,"type":"image\/png","animated":false,"width":900,"height":940,"size":48902,"views":0,"bandwidth":0,"vote":null,"favorite":false,"nsfw":null,"section":null,"account_url":null,"account_id":0,"is_ad":false,"in_most_viral":false,"has_sound":false,"tags":[],"ad_type":0,"ad_url":"","edited":"0","in_gallery":false,"deletehash":"Z9xFH8mrSH8lRDB","name":"","link":"https:\/\/i.imgur.com\/TvFtE29.png"},"success":true,"status":200}
but my problem i want to collect the https://i.imgur.com/TvFtE29.png into specific variable like $uploaded = 'https://i.imgur.com/TvFtE29.png'; to added into my database -> user pic
i went with json_decode but it's not completed with me in the right way, if someone can help with this issue 🌹
thanks.
json_decode worked fine here:
$json_returned = json_decode($json_returned, true);
$uploaded = $json_returned['data']['link'];
I'm trying to upload a movie to the Wistia API by CURL (http://wistia.com/doc/upload-api).
It works fine using the following command line, but when I put it in PHP code, I just get a blank screen with no response:
$ curl -i -d "api_password=<YOUR_API_PASSWORD>&url=<REMOTE_FILE_PATH>" https://upload.wistia.com/
PHP Code:
<?php
$data = array(
'api_password' => '<password>',
'url' => 'http://www.mysayara.com/IMG_2183.MOV'
);
$chss = curl_init('https://upload.wistia.com');
curl_setopt_array($chss, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_POSTFIELDS => json_encode($data)
));
// Send the request
$KReresponse = curl_exec($chss);
// Decode the response
$KReresponseData = json_decode($KReresponse, TRUE);
echo("Response:");
print_r($KReresponseData);
?>
Thanks.
For PHP v5.5.0 or later, here's a class for uploading to Wistia, from a LOCALLY STORED file.
Usage:
$result = WistiaUploadApi::uploadVideo("/var/www/mysite.com/tmp_videos/video.mp4","video.mp4","abcdefg123","Test Video", "This is a video upload demonstration");
The class:
#param $file_path Full local path to the file
#param $file_name The name of the file (not sure what Wistia does with this)
#param $project The 10 character project identifier the video will upload to
#param $name The name the video will have on Wistia
#param $description The description the video will have on Wistia
class WistiaUploadApi
{
const API_KEY = "<API_KEY_HERE>";
const WISTIA_UPLOAD_URL = "https://upload.wistia.com";
public static function uploadVideo($file_path, $file_name, $project, $name, $description='')
{
$url = self::WISTIA_UPLOAD_URL;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$params = array
(
'project_id' => $project,
'name' => $name,
'description' => $description,
'api_password' => self:: API_KEY,
'file' => new CurlFile($file_path, 'video/mp4', $file_name)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//JSON result
$result = curl_exec($ch);
//Object result
return json_decode($result);
}
}
If you don't have a project to upload into, leaving $project blank will NOT apparently force Wistia to create one. It will just fail. So you might have to remove this from the $params array if you don't have a project to upload to. I've not experimented to see what happens when you leave $name blank.
Your problem (and the difference between the command line and PHP implementation) is probably that you're JSON encoding the data in PHP, you should use http_build_query() instead:
CURLOPT_POSTFIELDS => http_build_query($data)
For clarity, the Wistia API says it returns JSON, but doesn't expect it in the request.
I'm trying to write a very simple test to upload an image to imgur and then redirect to the imgur page once it's done. (haven't gotten to that point yet)
I've looked all around these forums and tried so many different codes posted here, as well as cobbled some together from various posts. Finally, I've stopped getting errors in the php.
But.. in every code I've seen, it displays the image once it's done. Using that exact same code, my image does not appear, all I get is a page with Result: and nothing more. Does anyone have any idea what could be the problem?
<html>
<body>
<form action="upload_img.php" method="post" enctype="multipart/form-data">
<input type="file" name="imgupload" /><br>
<input type="submit" value="Upload to Imgur" />
</form>
</body>
</html>
and the php file:
<?php
$client_id = '$clientIDHERE';
$file = file_get_contents($_FILES["imgupload"]["tmp_name"]);
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
$json_returned = curl_exec($curl); // blank response
echo "Result: " . $json_returned ;
curl_close ($curl);
?>
Before you close the $curl handle you should check for errors:
if ($error = curl_error($curl)) {
die('cURL error:'.$error);
}
I've got the following code and it works perfectly fine for uploading one image to Imgur using their API:
$client_id = $myClientId;
$file = file_get_contents($_FILES["file"]["tmp_name"]);
$url = 'https://api.imgur.com/3/image.json';
$headers = array("Authorization: Client-ID $client_id");
$pvars = array('image' => base64_encode($file));
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL=> $url,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $pvars
));
$json_returned = curl_exec($curl); // blank response
$json = json_decode($json_returned, true);
curl_close ($curl);
However I need to upload multiple images at once. On the client side, the user will have multiple <input type="file" /> fields. I'm completely stuck now with figuring out where and how I will need to modify this code in order to handle multiple image upload when they come through to the server in the form of an array. Does anyone have any ideas?
Change the markup as follows:
<form action="file-upload.php" method="post" enctype="multipart/form-data">
Send these files:<br />
<input name="file[]" type="file" multiple="multiple" /><br />
<input type="submit" value="Send files" />
</form>
Now, you can loop through the $_FILES array using a foreach, like so:
foreach ($_FILES['file']['tmp_name'] as $index => $tmpName) {
if( !empty( $tmpName ) && is_uploaded_file( $tmpName ) )
{
// $tmpName is the file
// code for sending the image to imgur
}
}