2012-11-22 2 views
0

Curl을 사용하여 URL에 이미지를 업로드하고 싶습니다. 그러나 이미지 파일이 https url에 있으므로 fopen을 사용하여 파일을 읽을 수 없습니다.PHP : CURL PUT FOR Https fopen으로 작동하지 않는 URL이 없습니다.

코드는 다음과 같습니다.

$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); 
+0

나를 보자. 내 마법의 수정 구슬이 우리에게 새로운 달이 있다고 말하면서 코드가 작동하지 않는 이유입니다. 진심으로, 귀하의 질문에 표시되는 오류 메시지를 추가하십시오. –

답변

2

업로드하는 파일 데이터를 명명 된 키/값 쌍으로 전달할 필요가 없다고 가정하고 생각합니다. 대신

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

: PUT의 API 요청에 대한

curl_setopt($ch, CURLOPT_PUT, true); 

옵트 설정은 아래에이 설정을 사용하여 잘 작동합니다 :

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");