2016-06-30 9 views
7

मैं पोस्टमैन का उपयोग करके एक एपीआई एंडपॉइंट पर एक फ़ाइल अपलोड करने में सक्षम हूं।Guzzle 6 का उपयोग करके फ़ाइल अपलोड करें एपीआई एंडपॉइंट

मैं अनुवाद करने के लिए है कि एक रूप से एक फ़ाइल अपलोड, यह Laravel का उपयोग करने और अंत बिंदु guzzle 6 का उपयोग करने के लिए पोस्ट

यह कैसे डाकिया में लग रहा है का स्क्रीनशॉट (मैं जानबूझकर बाहर छोड़ दिया पोस्ट URL अपलोड करने में कोशिश कर रहा हूँ

POST /api/file-submissions HTTP/1.1 
Host: strippedhostname.com 
Authorization: Basic 340r9iu34ontoeioir 
Cache-Control: no-cache 
Postman-Token: 6e0c3123-c07c-ce54-8ba1-0a1a402b53f1 
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW 

----WebKitFormBoundary7MA4YWxkTrZu0gW 
Content-Disposition: form-data; name="FileContents"; filename="" 
Content-Type: 


----WebKitFormBoundary7MA4YWxkTrZu0gW 
Content-Disposition: form-data; name="FileInfo" 

{ "name": "_aaaa.txt", "clientNumber": "102425", "type": "Writeoff" } 
----WebKitFormBoundary7MA4YWxkTrZu0gW 

नीचे है फ़ाइल और अन्य जानकारी को बचाने के लिए नियंत्रक समारोह:) enter image description here

नीचे पाठ यह है जब आप डाकिया में "कोड बनाएं" लिंक पर क्लिक करें उत्पन्न करता है । फ़ाइल सही तरीके से अपलोड होती है, मैं फ़ाइल जानकारी प्राप्त करने में सक्षम हूं।

मुझे लगता है कि मेरे पास जो समस्या है, वह सही डेटा के साथ मल्टीपार्ट और हेडर सरणी सेट कर रहा है।

public function fileUploadPost(Request $request) 
{ 
    $data_posted = $request->input(); 
    $endpoint = "/file-submissions"; 
    $response = array(); 
    $file = $request->file('filename'); 
    $name = time() . '_' . $file->getClientOriginalName(); 
    $path = base_path() .'/public_html/documents/'; 

    $resource = fopen($file,"r") or die("File upload Problems"); 

    $file->move($path, $name); 

    // { "name": "test_upload.txt", "clientNumber": "102425", "type": "Writeoff" } 
    $fileinfo = array(
     'name'   => $name, 
     'clientNumber' => "102425", 
     'type'   => 'Writeoff', 
    ); 

    $client = new \GuzzleHttp\Client(); 

    $res = $client->request('POST', $this->base_api . $endpoint, [ 
     'auth' => [env('API_USERNAME'), env('API_PASSWORD')], 
     'multipart' => [ 
      [ 
       'name' => $name, 
       'FileContents' => fopen($path . $name, 'r'), 
       'contents'  => fopen($path . $name, 'r'), 
       'FileInfo'  => json_encode($fileinfo), 
       'headers'  => [ 
        'Content-Type' => 'text/plain', 
        'Content-Disposition' => 'form-data; name="FileContents"; filename="'. $name .'"', 
       ], 
       // 'contents' => $resource, 
      ] 
     ], 
    ]); 

    if($res->getStatusCode() != 200) exit("Something happened, could not retrieve data"); 

    $response = json_decode($res->getBody()); 

    var_dump($response); 
    exit(); 
} 

त्रुटि मैं प्राप्त कर रहा है, यह कैसे Laravel के डिबगिंग दृश्य का उपयोग कर प्रदर्शित करता है के स्क्रीनशॉट:

enter image description here

उत्तर

14

जिस तरह से आप डेटा पोस्ट कर रहे हैं वह गलत है, इसलिए डेटा प्राप्त विकृत कर रहा है।

Guzzle docs:

multipart का मूल्य साहचर्य सरणियों की एक सरणी, प्रत्येक निम्नलिखित महत्वपूर्ण मान जोड़ों युक्त:

name: (स्ट्रिंग, आवश्यक) प्रपत्र फ़ील्ड नाम

contents: (स्ट्रीम इंटरफेस/संसाधन/स्ट्रिंग, आवश्यक) फॉर्म तत्व में उपयोग करने के लिए डेटा।

headers: (सरणी) फॉर्म तत्व के साथ उपयोग करने के लिए कस्टम हेडर की वैकल्पिक सहयोगी सरणी।

filename: (स्ट्रिंग) वैकल्पिक स्ट्रिंग भाग में फ़ाइल नाम के रूप में भेजने के लिए।

उपरोक्त सूची में से कुंजी का उपयोग करना और प्रत्येक क्षेत्र को एक सरणी में अलग किए बिना अनावश्यक शीर्षलेख सेट करना परिणामस्वरूप खराब अनुरोध करेगा।

$res = $client->request('POST', $this->base_api . $endpoint, [ 
    'auth'  => [ env('API_USERNAME'), env('API_PASSWORD') ], 
    'multipart' => [ 
     [ 
      'name'  => 'FileContents', 
      'contents' => file_get_contents($path . $name), 
      'filename' => $name 
     ], 
     [ 
      'name'  => 'FileInfo', 
      'contents' => json_encode($fileinfo) 
     ] 
    ], 
]); 
संबंधित मुद्दे