2015-07-21 73 views
1


這頭將通過我的節目一個地址:,捲曲PHP上傳文件通過自定義標題

--55ae49448a20c 
Content-Disposition: form-data; name="chat_id" 
Content-Length: 9 

108432389 
--55ae49448a20c 
Content-Disposition: form-data; name="photo"; filename="Untitled.png" 
Content-Length: 16252 
Content-Type: image/png 

PNG 

我想下面的代碼POST 2個可變因素(chat_id)和(照片)。
嘗試代碼:

<?php 
$params = "Content-Disposition: form-data; name=\"chat_id\"\r\n" 
    . "Content-Length: 9\r\n\r\n" 
    . "\r\n" 
    . "Content-Disposition: form-data; name=\"photo\"; filename=\"Untitled.png\"\r\n" 
    . "Content-Length: ".filesize("Untitled.png")."\r\n" 
    . "Content-Type: image/png\r\n\r\n" 
    . file_get_contents("Untitled.png"); 
$request_headers = array(); 
$request_headers[] = 'Content-Length: ' . strlen($params); 
$url  = 'http://example.com'; 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $params); 
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); 
$reply = curl_exec($ch); 
?> 

是否有任何其他方式爲做到這一點!?
我的企圖編碼給我400錯誤的GATWAY錯誤..

回答

0

這就是完全不必要的。你做得太多了,CURL可以做得更好(並且恰當)。

$post = array(
    'chat_id' => 108432389, 
    'photo' => '@Untitled.jpg' 
); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 

old-school php curl在值字段中使用@來表示要上傳的文件。較新版本改用curlfile

+0

謝謝,但它不能在電報API工作: '<?PHP的 $交=陣列( 'chat_id'=> 108432389, '照片'=>真實路徑( 'ur.png') ); $ url ='https://api.telegram.org/(MyToken)/SendPhoto'; $ ch = curl_init($ url); $ headers = array(「Content-Type:multipart/form-data」); curl_setopt($ ch,CURLOPT_POST,1); curl_setopt($ ch,CURLOPT_POSTFIELDS,$ post); curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ ch,CURLOPT_HTTPHEADER,$ headers); $ reply = curl_exec($ ch); ?> –