I am using curl to download profile photo. Is that right method? - php

function getFacebookPhoto($img,$fullpath) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_URL, $img);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 0);
$user_curl_image = curl_exec($curl);
curl_close($curl);
if(file_exists($fullpath)){
#unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $user_curl_image);
fclose($fp);
}
This is the script i use to download photos.
Is it rightway to do it?
I use this function inside facebook apps.

Related

Why can't get the length of the pdf file with `CURLINFO_CONTENT_LENGTH_DOWNLOAD` in php_curl?

I have to get filesize of a remote file by using curl in PHP
the file is hosted on Dropbox which will be downloaded by direct link
below is my code what I have written
$szUrl='https://www.dl.dropboxusercontent.com/s/8ol4o753smure2o/Holy_Quran_in_Turkish.pdf';
$curl = curl_init();
curl_exec($curl);
curl_setopt($curl, CURLOPT_URL, $szUrl);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_NOBODY, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$size = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
echo $size;
curl_close($curl);
The output is -1 with the code, How to get the length of the iso file properly with php_curl?
You have to set the curl options before you exec:
<?php
$szUrl='https://www.dl.dropboxusercontent.com/s/8ol4o753smure2o/Holy_Quran_in_Turkish.pdf';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $szUrl);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_NOBODY, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($curl);
$size = curl_getinfo($curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
echo $size;
curl_close($curl);

How to get JSON data from Rest API by PHP Curl?

I have a Rest api which I can access by this url: "http://127.0.0.1:8000/api/thesis/?format=json". Now I want to get the JSON data from it. For connecting to the api I tried to use PHP-Curl as below. But I get NULL! (This is the first time I'm doing php any help will be great!)
<?php
$service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
//initialize a curl session
$curl = curl_init();
//set options for the transfer
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_TIMEOUT, 5);
curl_setopt($curl, CURLOPT_URL, $service_url);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
//execute the session
$curl_response = curl_exec($curl);
//finish off the session
curl_close($curl);
$curl_jason = var_dump(json_decode($curl_response, true));
print_r($curl_jason);
echo $curl_jason;
?>
I think you should use GET method of curl like this
$service_url = "http://127.0.0.1:8000/api/thesis/?format=json";
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
//execute the session
$curl_response = curl_exec($curl);
//finish off the session
curl_close($curl);
$curl_jason = json_decode($curl_response, true);
print_r($curl_jason);
Use the below mentioned code snippet to fetch data from a REST API using PHP curl
<?php
function _isCurl(){
return function_exists('curl_version');
}
if (_iscurl()){
//curl is enabled
$url = "http://testDomainName/restAPI.php?id=123&amt=100&jsonp=?";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
print_r($output);
// Curl operations finished
}
else{
echo "CURL is disabled";
}
?>

Parse web page without save local?

Sorry my English a little. I am using CURL because web page is required this function. I don't get file_get_contents of page. How to parse page without page save? (fopen,fwrite)
<?PHP
function fileGet($url, $timeout = 55, $ref=true){
$useri = $_SERVER['HTTP_USER_AGENT'];
#set_time_limit($timeout);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl, CURLOPT_COOKIE, 'PHPSESSID=fztitsfgsafafaq25llwafd0; path:/' );
curl_setopt($curl, CURLOPT_USERAGENT, $useri);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_REFERER,$url);
$data = curl_exec($curl);
curl_close($curl);
// Save Page Start
$fp = fopen('data.html', 'w');
fwrite($fp, $data);
fclose($fp);
// Save Page End
return $data;
}
// Start Code
fileGet("http://www.example.com",10); // Start Function
$html = file_get_html('data.html'); // Open Saved Page In Local
foreach($html->find('div.columns') as $article) {
// Events.....
mysql_query("Insert Query");
}
// End Code
?>
<?PHP
function fileGet($url, $timeout = 55, $ref=true){
$useri = $_SERVER['HTTP_USER_AGENT'];
#set_time_limit($timeout);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl, CURLOPT_COOKIE, 'PHPSESSID=fztitsfgsafafaq25llwafd0; path:/' );
curl_setopt($curl, CURLOPT_USERAGENT, $useri);
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_REFERER,$url);
$data = curl_exec($curl);
return $data;
}
// Start Code
$html = str_get_html(fileGet("http://www.example.com",10));
foreach($html->find('div.columns') as $article) {
// Events.....
mysql_query("Insert Query");
}
// End Code
?>

How to send a file with curl to the controller

I have an url with an image: http://www.example.com/image.jpg
and I have an action in the controller that uploads a file to the server by user input. I want to use this same action to upload a file by url. I started to get the data from image:
$url = "http://www.example.com/image.jpg";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
Now how do I send the $rawdata to the controller to be handled like a normal user upload?
Thanks
I figured it out myself.
$url = "http://www.example.com/image.jpg";
$filename = basename($url, rand_string(7));
$path = realpath("../webroot/uploads/")."/".$filename;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec($ch);
curl_close ($ch);
if(file_exists($path)){
unlink($path);
}
$file = fopen($path, 'x');
fwrite($file, $rawdata);
fclose($file);
$post_data['Filedata'] = '#'.$path;
$url = "http://localhost/controller/action";
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);
echo $response;
In the controller:
function action(){
//get properties of the file
//$_FILES['Filedata']['name'];
//$_FILES['Filedata']['size'];
//$_FILES['Filedata']['tmp_name'];
//savefile();
}
If anyone knows how to post the file without saving it will be great.

Upload a file using an API PUT request

I'm building an API in PHP. One of the methods is place.new (PUT request). It expects several string fields, and it also expects an image. However I can't get it working. With a POST request it was easy, but I'm not sure how to do it with a PUT and how to get the data on the server.
thanks for the help!
Test CURL code
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $this->url);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_INFILE, $image);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($image));
$this->result = curl_exec($curl);
curl_close($curl);
Server code
if ( $im_s = file_get_contents('php://input') )
{
$image = imagecreatefromstring($im_s);
if ( $image != '' )
{
$filename = sha1($title.rand(11111, 99999)).'.jpg';
$photo_url = $temp_dir . $filename;
imagejpeg($image, $photo_url);
// upload image
...
}
}
Solution
send
// Correct: /Users/john/Sites/....
// Incorrect: http://localhost/...
$image = fopen($file_on_dir_not_url, "rb");
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_INFILE, $image);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file_on_dir_not_url));
$result = curl_exec($curl);
curl_close($curl);
receive
/* Added to clarify, per comments */
$putdata = fopen("php://input", "r");
/* Open a file for writing */
$fp = fopen($photo_url, "w");
/* Read the data 1 KB at a time
and write to the file */
while ($data = fread($putdata, 1024))
{
fwrite($fp, $data);
}
/* Close the streams */
fclose($fp);
fclose($putdata);
Did you read http://php.net/manual/en/features.file-upload.put-method.php ? Script PUT /put.php all set up?
Also, what is $image -- it needs to be a file handler, not a file name.
Ps. Using file_get_contents will try to load whatever is PUT on the server into memory. Not a good idea. See the linked manual page.

Categories