I followed the google api examples for uploading to youtube. However if i want to upload a file that hosted on another domain for example:
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();
// create a new Zend_Gdata_App_MediaFileSource object
$filesource = $yt->newMediaFileSource('http://www.myotherdomain.com/.../file.mov');
$filesource->setContentType('video/x-flv');
// set slug header
$filesource->setSlug('http://www.myotherdomain.com/.../file.mov');
// add the filesource to the video entry
$myVideoEntry->setMediaSource($filesource);
Any help would be greatly appreciated
The Zend API probably doesn't support this. However, if you have write-permissions, you can download the file on your server and then send it.
$curl = curl_init();
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_URL, 'http://www.myotherdomain.com/.../file.mov');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$contents = curl_exec($curl);
curl_close($curl);
$file = fopen('temp/file.mov', 'w'); // If file is not found, attempts to create it
fwrite($file, $contents);
fclose($file);
// Upload via YouTube
Related
I've got a website A that holds an image, and I am now using PHP to grab that image and I want to post it via form-data media to another website.
My solution now is to save the image as a file and then use CURLFile to auto-generating these form-media post fields.
Is there a way that I don't need to save this image to my local file system?
What I am doing now:
$img = urlcurl::geturl($this->url); // seems to be an raw image data (png?)
$fp = fopen('./tmp/1.png','x');
fwrite($fp, $img);
$t = new CURLFile("/tmp/1.png","image/png"); // get from local file storage
$filedata = array("media" => $t);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
$response = curl_exec($ch);
Cannot grab pictures from the specific site with PHP, but with PYTHON is working for this site, how to download images via PHP?
image URL is https://www.autoopt.ru/product_pictures/big/bcb/054511.jpg
If I paste another URL, of another site, the picture is downloading, but this site doesn't work.
i try with file put content and so on, my last code is
<?php
function downloadImage($img_url){
$image = file_get_contents($img_url);
$img_save_path = realpath(dirname(__FILE__)) . '/assets/upload_products/';
$image_name = basename($img_url);
$image_fullpath = $img_save_path.$image_name;
$ch = curl_init ($img_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$raw=curl_exec($ch);
curl_close ($ch);
if(file_exists($image_fullpath)){
unlink($image_fullpath);
}
$fp = fopen($image_fullpath,'x');
fwrite($fp, $raw);
fclose($fp);
return true;
}
downloadImage('https://www.autoopt.ru/product_pictures/big/bcb/054511.jpg');
You dont need to do all that to save the picture if your going to use file_get_contents. It can be done by only using:
file_put_contents($image_fullpath, file_get_contents($img_url));
Also, like mentioned by OMi Shash in the comments, you'll need to pass header info to file_get_contents for it to work with that url. I've just done the test and it worked.
//set header info
$opts = array('http'=>array('header' => "User-Agent:MyAgent/1.0\r\n"));
//Basically adding headers to the request
$context = stream_context_create($opts);
file_put_contents($image_fullpath, file_get_contents($img_url, false, $context));
I have a situation, where in my project for file upload, i have to send that image to java service(in multipart format), so that they can upload image from their side.
I have tried sending that image to java service in multiple ways,
I have used curl methods to send file to java service.
But no method worked for me.
If we do File uploading via postman, its hitting java service and successfully uploading the image.
I have mentioned a sample code which i have tried.
If anyone knows how to do this.
$path = 'http://10.10.10.113:8765/cojoin-service/api/image/upload';
$arrInputs = $arrKeyset;
if ( isset($_FILES['fileToUpload']) ) {
$filename = $_FILES['fileToUpload']['tmp_name'];
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $path);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close ($curl);
I'm trying to load and save a remote PDF to my server for a project, but the link has no file extension. It's a kind of secured link.
https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=F86A56B3-D12C-4E70-AE71-A8A422B3EA4E&LINK_PAGE=ITINERARYENC&TRANS_ID=A25D191B-B098-4F45-9217-FB6D2B70F803
When I open the link in the browser I can download the PDF file, but saving it with my script has no success.
Is there a way to save the PDF on my server with a script?
I tried following code without success:
$url ="https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=F86A56B3-D12C-4E70-AE71-A8A422B3EA4E&LINK_PAGE=ITINERARYENC&TRANS_ID=A25D191B-B098-4F45-9217-FB6D2B70F803";
getFileContents($url);
function getFileContents($url)
{
// Workaround: Save temp file
$img = tempnam(sys_get_temp_dir(), 'pdf-');
$img .= '.' . pathinfo($url, PATHINFO_EXTENSION);
$fp = fopen($img, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
return $result ? $img : false;
}
I found the script here : Downloading a large file using curl
To download a file (binary or not) you can use file_get_contents().
file_get_contents return the file content into a string.
//Download PDF content
$pdfContent = file_get_contents("https://www.enablelogistics.com.au/BECPRD/SSOAuth.aspx?SESSION_KEY=XXXXXXX-DDDD-4444-AAAA-XXXXXXX&LINK_PAGE=ITINERARYENC&TRANS_ID=AAAAAAAA-3333-5555-6666-222222222");
$fileName = "myPDF.pdf";
$fp = fopen($fileName, 'w+');
//Write content into the file
fwrite($fp, $pdfContent);
fclose($fp);
You can if necessary use options in file_get_contents to specify GET or POST method, Basic Authentication and more.
Thanks for your reply, but your code doesn't saves the PDF im looking for.
It safes a PDF with HTML content :
WWW.ENABLELOGISTICS Enable Logistics v95.4248
Welcome to Enable
Logistics Please enter your username and password to sign in.
Forgot Password?©2016 Bright People Technologies Pty Ltd
I have no clue how to dowload the pdf to my server.
Clicking the link only let's me download local.
I try to find a solution to transfer image to remove server with php..
For Example i have image http://example.com/image.jpg.. and want to transfer this link/file to http://postimage.org or any other image hosting service.. if you have any solution in PHP Please let me know..
EDIT:
After transfer image file its shows me the download link.. Special for http://postimage.org site..
Thanks,
Post image has an upload from url feature:
http://postimage.org/index.php?um=url
Enter the url to your image there and submit.
This is a very open question, but I'll try to answer it.
If you want to upload pictures to a image hosting site, you need to search for one that has an API.
I would recommend imgur for starting.
It has free (limited) access to its API by just registering and it is very simple to use.
Read about it here.
It even has an example for uploading images in PHP:
<?
$filename = "image.jpg";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));
// $data is file data
$pvars = array('image' => base64_encode($data), 'key' => IMGUR_API_KEY);
$timeout = 30;
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://api.imgur.com/2/upload.xml');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);
$xml = curl_exec($curl);
curl_close ($curl);
?>