This question already has answers here:
Intervention Image: Save image directly from an url with original file name and ext?
(3 answers)
Closed 2 years ago.
I have an API project using Laravel and these requires me to have a download and save it to public folder and in database.
Flow:
A field will come through the API call, i.e. through the company_logo field. It will come through as a URL: http://a.mktgcdn.com/p/Lr5Sfx0OllncmEPfQGX11ZqUZSmcUhbvc5a-u-FrMCU/1.0000/300x300.jpg. I need to grab the image and save it locally to a unique folder per listing and save it also to database.
I don't have code yet because I don't know what to do here in Laravel.
You can simply do that by using Intervention Image
You can find how to install here
$path = 'http://a.mktgcdn.com/p/Lr5Sfx0OllncmEPfQGX11ZqUZSmcUhbvc5a-u-FrMCU/1.0000/300x300.jpg';
$filename = basename($path);
Image::make($path)->save(public_path('yourimagefolder/' . $filename));
this will save the image. and following code will save it to database
$image = new ModelName();
$image->imagename =$filename;
$image->save();
Hope this help, you need to define $url and $filename variable
$data = file_get_contents( $url );
file_put_contents( public_path($filename), $data );
Related
I have been trying to use the google api client in cakekphp 3, but not successful on adding the service account json file to my project.
The below line is mentioned in:
https://github.com/googleapis/google-api-php-client
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
But that doesn't work on cakephp 3. I know I can add it to the .env.default file, but it is not recommended for production.
How to use it then?
This function defines a default credentials setting path to be used, but note that you have not called it yet.
You must "tell" your application to use its default credentials settings.
// app.php
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
// In the file you want to use Google Client
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
CakePHP also can read an specific file for env variables:
// config/.env
export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account.json"
// Uncomment this part of your bootstrap.php
if (!env('APP_NAME') && file_exists(CONFIG . '.env')) {
$dotenv = new \josegonzalez\Dotenv\Loader([CONFIG . '.env']);
$dotenv->parse()
->putenv()
->toEnv()
->toServer();
}
Keeping your setup on .env files make it easier maintaining all your settings up to date.
I am trying to get the url of an image attachment published by a facebook page. The goal is to embed that image in a webpage in order for the website to always display the last image attachment published by the page. I own both website and FB page.
I have yet not grasped all the details on handling Facebook Graph API, but here is what I did so far:
1) in FB developers website, I have created an application, getting its App ID and secret;
2) I used that information to get an access token (just pasted in my browser the following code:
https://graph.facebook.com/oauth/access_token?client_id={my-client-id}&client_secret={my-client-secret}&grant_type=client_credentials
3) in my website, I have loaded Facebook JS SDK after the body tag; and got also my Facebook page ID from Facebook Page administration;
4) now the real question begins: how can I query Facebook to get the information that I need – the source url of the last published image?
The best result I have gotten so far was by making a getJSON call with the help of jQuery:
var fbfeed = $j.getJSON('https://graph.facebook.com/{my-page-id}/feed?access_token={my-token}&fields=attachments&limit=1');
This will get and store a JSON array in the fbfeed variable (please correct me if I'm wrong). One of the keys of that array is called "src" which contains the source url of the attachment – the information I need to embed that picture in my website;
I have the following problems / concerns:
- I have not found the way to retrieve the value of the "url" key – how can I do that? How can I parse the fbfeed variable and extract the value of the "url" key?
– I have concerns with my usage of the access token:
is it problematic to expose the access token in this way, by using it in a jQuery function? Is it a security risk? If so, can I "mimic" this request but using a server side language such as PHP?
Will this access token expire (i.e. will I need to repeat step 2 from time to time?). So, imagining that I can get this to work, will I need from time to time to "refresh" the access token?
Thanks for your help.
I have managed to get the information I needed using server-side code, although it may not be the most "clean solution": it will iterate through the last 5 posts of my page until it finds an image and a post url:
<?php
$url = 'https://graph.facebook.com/{page-id}/feed?access_token={access-token}&fields=attachments,link&limit=5';
$json = file_get_contents($url);
$json_data = json_decode($json, true);
for($count = 0; $count < 5; $count++) {
$imagesource = $json_data['data'][$count]['attachments']['data'][0]['media']['image']['src']; // gets the image url
$postlink = $json_data['data'][$count]['link']; // gets the post url
if (isset($imagesource) && isset($postlink)) {
// do stuff with the image and post url
break;
};
};
// then I can do other stuff as fallback if the image url and post url are not found
I am trying to figure out how to make a simple html code so that whenever anyone on the page types anything into the provided text box and hits submit, it adds that written text to an already existing .txt file on my server.
UPDATE 2/20/14 9:29AM: Well that's unfortunate. I kind of figured I required a .php but sadly my wepbage is hosted through homestead and they do not have .php functionality. Was just hoping there was a workaround. Thanks for the responses.
If your server can run php then the following page can be requested when the user clicks submit. (using post method)
<?php
$in = $_POST['name'];
$file = 'names.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= $in;
// Write the contents back to the file
file_put_contents($file, $current);
?>
You would have to use PHP to do this. Make the form action on your form link to a PHP script and inside have something like this.
<?php
$file = 'test.txt';
$currentText = file_get_contents($file);
$currentText .= $_POST['text'];
file_put_contents($file, $currentText);
?>
After turning on Google Drive API access from the management console and getting my Client ID keys, I followed the sample code (using Python 2.7) and I am able to insert a folder, set the appropriate permissions (type=anyone,role=reader), and insert a text/html type file into the new folder.
However the JSON file resource objects I receive from executing insert on the drive service have no 'webViewLink' field! There are 'webContentLink' and 'selfLink' fields but 'webViewLink', which is necessary for static HTML publishing, seems to be missing.
Most perplexing. If this feature hasn't been turned on yet or if I need to configure my account settings to allow HTML publishing please let me know. Any other help would be most appreciated ;)
The webViewLink property is only returned for public folders, and not the single files inside such folders. You can use that as the base url to construct links to your files.
The WebViewLink file property can be retrieved by doing something like this:
$file = $service->files->get($file_id, array('fields' => 'webViewLink'));
$web_link_view = $file->getWebViewLink();
OR
$sheetsList = $drive_service->files->listFiles([
'fields' => 'files(id, name, webViewLink, webContentLink)',
]);
$web_link_view = $sheetsList->current()->getWebViewLink();
Pay attention that you should load the file specifying which fields you wanna bring with it (In this case, webViewLink). If you don't do that, only id and name will be available.
If you also need to configure file permissions, you can do something like:
$permissions = new \Google_Service_Drive_Permission();
$permissions->setRole('writer');
$permissions->setType('anyone');
$drive_service->permissions->create($file_id, $permissions);
Possible values for setRole() and setType() can be found here: https://developers.google.com/drive/api/v3/reference/permissions/create
I am using the following Google Maps Geocoding service via a php script
http://maps.googleapis.com/maps/api/geocode/xml?address=
I have already tried curl and file_get_contents but none of them worked. The problem is when I use city name with one word it works but when I use city name with two words then I do not get anything back from the service but the following error
That’s an error.
Your client has issued a malformed or illegal request. That’s all we know.
Below is some code snippet from the script
$city = $_GET["c"];
$url="http://maps.googleapis.com/maps/api/geocode/xml?address=$city&sensor=false";
$data = curl_download($url);
//$data = file_get_contents($url);
echo $data;
I have also uploaded the script to the location below
01)http://www.javeria.com/sites/travel/map/googlemap.php?c=Houston (works)
02)http://www.javeria.com/sites/travel/map/googlemap.php?c=New+York (does not work)
When invoked with 02) it won't display the map instead I am masking the error to display a user friendly message.
I would appreciate any help into this matter.
Try using + between the words. I am doing essentially the same thing - and that change makes it work for me.