download a file from PHP Curl - php

I have this code in my curl:
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'User-Agent: '.$_SERVER['HTTP_USER_AGENT'],
'X-Forwarded-For: '.$this->getIp()
));
$result = curl_exec($ch);
return $result;
I am getting this giberish result:
I am expecting an image file.
Question here is that how can i download this image file?

you can use this code
$profile_Image = $url; //image url
$userImage = 'myimg.jpg'; // renaming image
$path = ''; // your saving path
$ch = curl_init($profile_Image);
$fp = fopen($path . $userImage, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);

you can try this:
$data=send_curl($url);
$path='./test.jpg';
$file = file_put_contents($path,$data);
the function of send_curl
function send_curl($url, $data, $PROXY = "") {
$ch = curl_init ();
if ($PROXY != "") {
curl_setopt ( $ch, CURLOPT_PROXY, $PROXY );
}
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}

Ideally, browsers decide how to display a given piece of content using the information of the Content-Type HTTP header and PHP can generate HTTP headers with the header() function.
If you know for sure that your picture will always be in JPEG format it's rather straightforward:
header('Content-Type: image/jpeg');
echo your_download_function();
exit;
Otherwise, you need to fetch the header returned by the remote server. Curl has a tricky syntax—I refer you to PHP cURL retrieving response headers AND body in a single request?

Related

php post data using curl

When I post the data using ajax and send to post.php by curl then $_FILES variable is empty and get the data in $_POST variable. When I print $_POST var getting the following data on post.php.
[temp_upload_file] => #/tmp/php6OHgQc;filename=Penguins.jpg;type=image/jpeg
when I print "$_FILES" var getting empty data on post.php
Array
(
)
Code:
$url = "post.php";
$ch = curl_init($url);
// send a file
curl_setopt($ch, CURLOPT_POST, true);
$data = array('temp_upload_file' =>'#'.$_FILES['uploadfile']['tmp_name'].';filename='.$_FILES['uploadfile']['name'].';type='. $_FILES['uploadfile']['type']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
echo curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
What I am doing wrong here??
Thank you in advance
For php5.5+ there is a new curl_file_create() function you need to use
the # format is now deprecated.
Try using following code:
$url = "post.php";
$tmpfile = $_FILES['temp_upload_file']['tmp_name'];
$filename = basename($_FILES['temp_upload_file']['name']);
$ch = curl_init($url);
// send a file
curl_setopt($ch, CURLOPT_POST, true);
$data = array(
'uploaded_file' => curl_file_create($tmpfile, $_FILES['uploadfile']['type'], $filename)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// output the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
echo curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

php + curl issue Resource id # 2 on curl_init

php + curl issue Resource id # 2 on curl_init:
$url = "https://example.com:4433/deviceservice/authorize?login=query"; // URL JSON
$ch = curl_init($url);
echo $ch; //write Resource id # 2
if( $ch )
{
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE);
$json = curl_exec( $ch );
$json = json_decode($json);
} else {
echo 'nothing';
}
What am I doing wrong?
If you are not on a host with SSL so you should bypass the SSL verification
<?php
$url = "https://example.com:4433/deviceservice/authorize?login=query";
$ch = curl_init($url);
echo $ch; //write Resource id # 2
if( $ch )
{
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$json = curl_exec( $ch );
$json = json_decode($json);
} else {
echo 'nothing';
}
Try to use curl_error($ch) and echo to diagnose the error
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
)
);
$response = curl_exec($ch);
$err_status = curl_error($ch);
echo $err_status;
curl_close($ch);
curl_init returns a cURL handle on success, FALSE on errors.
So echo $ch; will return something like Resource id #2.
See http://php.net/manual/en/function.curl-init.php
You have to try something like this
$url = "https://example.com:4433/deviceservice/authorize?login=query"; // URL JSON
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, TRUE);
$json = curl_exec( $ch );
$json = json_decode($json);
curl_close($ch);
if(empty($json)){
echo 'nothing';
}

PHP cURL Post request not working

I having issues running the correct cURL request. I am meant to do a Post request to the URL.
The example only runs a command line cURL request
$ curl -i -X POST {URL}
The issue I am running the following code and I am getting '400 Bad Request'
$ch = curl_init();
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
$output = curl_exec( $ch );
curl_close( $ch );
Can anyone help with sending the request correctly.
You're missing the CURLOPT_POSTFIELDS you wanna get by the request.
As explained here you're gonna need to set those fields:
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") { ... } else { ... }
?>
Try this:
POST Function:
function httpPost($url,$params)
{
$postData = '';
//create name value pairs seperated by &
foreach($params as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
Calling the Function:
$params = array(
"name" => "Ravishanker Kusuma",
"age" => "32",
"location" => "India"
);
echo httpPost("http://hayageek.com/examples/php/curl-examples/post.php",$params);
See, if that helps. Ref: link
curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $array );
Check the manual
To post, just make use of following curl options and try.
(if your url is - "https") {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //if required.
curl_setopt($ch, CURLOPT_URL, );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
return curl_exec($ch);
I guess the url you are passing doesn't have post fields. eiter you can make use of CURLOPT_POSTFIELDS and your post params their or else attache it to the url using http_build_query(post params)
http://www.url.com?arg1=val1&arg2=val2
Also after spending 5 hours at same code i just found the solution of my problem
If you are using Array in post Fields you have to json_encode that array to recognize as POST parameters
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
So This is Working CURL Code WITH POST Request
Give a Rep+ If my

php: curl script working but not in php file

I have a curl script that is working fine in terminal.
curl -XPOST \
-F "file=#var/www/myfile.wav;type=audio/wav" \
-H 'Authorization: Bearer $MY_TOKEN' \
'https://myurl'
I have tried this way in my php file,
<?php
$url = "https://myurl";
$key = "myKey";
$path = "/var/www/";
$fileName = "myfile.wav";
$data['file']= "#".$path.$fileName;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Authorization: Bearer $key"));
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>
I get an error from the server. Seems I am not making request as the server expects. Am I missing anything in the php script?
Try this:
$data = array('file' => file_get_contents($path.$fileName));
There may be a problem with your CURL library that PHP is using.
Or PHP may be blocked from making HTTPS connections.
Try a series of simple tests using CURL in PHP like this first one with POST false:
<?php
$url = "https://myurl/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
header("Content-Type: text/plain");
echo $response;
?>
and then with POST true...
<?php
$url = "https://myurl/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
header("Content-Type: text/plain");
echo $response;
?>
Since it's an HTTPs URL , You need to make use of this parameter
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

PHP: CURL PUT FOR Https Url not working as fopen fails

I want to upload the image to the url using Curl.
But as the Image file is on https url i am not able to read the file using fopen.
Code is as below.
$file = "https://xyz.com/image.jpg";
$url = "http://abc.com/upload.php";
$fp = fopen($file, "r");
$headers = array("Content-Type: xml");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
I think that this should work assuming that you don't need to pass the file data that you're uploading in as a named key/value pair.
$file = "https://xyz.com/image.jpg";
$url = "http://abc.com/upload.php";
$fileData = file_get_contents($file);
$fp = fopen($file, "r");
$headers = array("Content-Type: xml");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fileData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
curl_close($ch);
Instead of:
curl_setopt($ch, CURLOPT_PUT, true);
opt setting for PUT API request will work fine on using this setting below:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");

Categories