Abhishek Sanghvi
Abhishek Sanghvi

Reputation: 4651

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);

Upvotes: 0

Views: 814

Answers (2)

ram
ram

Reputation: 2343

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");

Upvotes: 0

James C
James C

Reputation: 14149

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);

Upvotes: 2

Related Questions