Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
User can paste image url on form, then my app download this on local server. I would like validate this image, but i have problem.
Input:
$image = "http://example.com/image.png";
Rules, but not working
'image' => [
'required',
'url',
'mimes:jpeg,png',
'max:5120',
'dimensions:min_width=400,min_height=400',
new FileExtensionRule(['png', 'jpg', 'jpeg'])
]
How can I validate image from external source.
First you have to make custom rule Custom Validation Rules
php artisan make:rule RemoteImage
and then put your logic inside this function in the RemoteImage class
public function passes($attribute, $value)
{
return strtoupper($value) === $value;
}
to get file type
$image = "http://example.com/image.png";
$ext = pathinfo($image, PATHINFO_EXTENSION);
to get the file size to validate
using curl
$ch = curl_init('http://example.com/image.png');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE);
$data = curl_exec($ch);
$size = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
echo $size;
Related
I have one Image on another server (Image).but when i get this image With file_get_contents() function it will return
Not Found Error
and generate this Image.
file_put_contents(destination_path, file_get_contents(another_server_path));
plz help me. if there are another way to get those image.
Try this.
There is problem with URL Special character.then you have to decode some special character from url basename.
$imgfile = 'http://www.lagrolla.com.au/image/m fr 137 group.jpg';
$destinationPath = '/path/to/folder/';
$filename = basename($imgpath);
$imgpath = str_replace($filename,'',$imgpath).rawurldecode($filename);
copy($imgfile,$destination_path.$filename);
Another way to download copy file from another server is using curl:
$ch = curl_init('http://www.lagrolla.com.au/image/data/m%20fr%20137%20group.jpg');
$destinationPath = '/path/to/folder/filenameWithNoSpaces.jpg';
$fp = fopen($destinationPath, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
Note: It is bad practice to save images with spaces in file name, so you should save this file with proper name.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How can I automatically upload to a database data from tables like the one in this page? I can use their function "export" and then manually download the .csv file, and upload it, but if I want everyday the data from each game, it is a pain... do you think it is possible to automatize it? The only solution would be by scraping their website?
Thanks
You could use the cURL lib of PHP.
Here there's an example:
<?php
$ch = curl_init();
$timeout = 0; // set to zero for no timeout
$url = 'http://www.basketball-reference.com/boxscores/201506160CLE.html'; // set the page url
curl_setopt ($ch, CURLOPT_URL, $url);//enter your url here
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch); //get the page contents
include "simple_html_dom.php";
$table = $file_contents;
$html = str_get_html($table);
$printing = false;
//header('Content-type: application/ms-excel');
$fp = fopen("php://output", "w");
foreach($html->find('tr') as $element){
$arr = array();
foreach ($element->find('tr') as $element2) {
if(!$printing)
$printing = strpos($element2,'Scoring') !== false;
if($printing){
//echo $element2 -> plaintext . "<br>";
$arr[] = $element2 -> plaintext; //comment here
}
}
fputcsv($fp, $arr);
}
fclose($fp);
?>
You have to download a file from here.
You get a text/csv file in your page, if you prefere a plain it's sufficient switch comments (comment line where is requested and decomment the others)
You can parse your new csv as you want
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
http://accounts.fstatic.org/json/results/?callback=google
I need extract all results from here.
Edit: Extract results by id: example
$result1 = result['1'];
$result2 = result['2'];
Use curl to fetch data and decode the json to array in php.
<?php
$json=fetchPageFromURL('http://accounts.fstatic.org/json/results/?callback=google');
$obj=json_decode($json,true);
print_r($obj);
echo "<hr>";
foreach ($obj as $key => $value) {
foreach (array('result','url','v-url','title','content') as $nnn => $keyname) {
if(0===strpos($key, $keyname)){
$id=substr($key, strlen($keyname), strlen($key)-strlen($keyname));
$fin[$id][$keyname]=$value;
}
}
}
print_r($fin);
function fetchPageFromURL($url){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
?>
Updated to fetch details and put them away.
$results = json_decode(file_get_contents('http://accounts.fstatic.org/json/results/?callback=google'));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to create a function that takes a URL parameter and a username parameter that goes to that url, enters the username and if the page says that the username doesn't exist(error) it will return false, otherwise it will return true. So I tried and I did something a little like this, but it didnt work.
function ($url, $username) {
$main = file_get_contents($url.$username);
if (#$main) { return false; }
else { return true; }
}
So if you have any ideas on how to make this idea actually work please help me
$site = 'https://twitter.com/';
$username = 'SteveMartinToGo';
$url = $site.$username;
function urlExists($url=NULL) {
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode >= 200 && $httpcode < 400){
return true;
} else {
return false;
}
}
if(urlExists($url)) {
echo 'url exists';
} else {
echo 'url does not exist';
}
The problem with this is that some sites (like Facebook) will return a 200 instead of a 404, so the URL will show as existing even though it's not. Also, I got this function from someplace else (can't remember where though) so I don't want to take credit for that code. hope it helps...
Edit: updated because of fred-ii's eagle eye and suggestions. :)
It is important to have the actual structure of the url. You can make a print and view the source code (HTML) if there is no special characters in the URL can be masked.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Failing to upload file with curl in heroku php app
After handling a file upload in php, I am trying to send that file using curl to my rest api which uses Slim Framework. However, $_FILES is always empty once it reaches the function in Slim.
sender.php
if(move_uploaded_file($_FILES['myFile']["tmp_name"], $UploadDirectory . $_FILES['myFile']["name"] ))
{
$ch = curl_init();
$data = array('name' => 'test', 'file' => $UploadDirectory . $_FILES['myFile']["name"]);
curl_setopt($ch, CURLOPT_URL, 'http://localhost/slimlocation/upload/');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
}
And the function to receive the request in Slim:
$app->post('/upload/', function() use ($app) {
if(isset($_FILES)){
// count is always zero
echo count($_FILES);
}
});
Am I sending the file incorrectly and / or is it possible to do what I am attempting? Any help is appreciated.
As far as i know, you need to use more options for a file upload with curl. See here.
Look at the CURLOPT_UPLOAD option and the description of CURLOPT_POSTFIELDS, it says that you need to use an # before the file name to upload (and use a full path).
These were the changes I needed and it worked:
$filepath = str_replace('\\', '/', realpath(dirname(__FILE__))) . "/";
$target_path = $filepath . $UploadDirectory . $FileName;
$data = array('name' => 'test', 'file' => '#'.$target_path);