Home Unable to call an REST API for FileUpload in php
Reply: 1

Unable to call an REST API for FileUpload in php

Juned Ansari
1#
Juned Ansari Published in 2017-12-07 14:09:36Z

There is an API build in C# and i am successfully able to to call that api from postman but the same thing i want to do with php.

Postman

Header info need to set before call api

Actual File Upload in attachment parameter.

Using Postman Tool I am able to upload file successfully.

But The Problem is Same thing i want to do using php Code.

What I Tried in PHP CODE:

$headers = array('DistributorCode:'.$_SERVER['HTTP_DISTRIBUTORAPIKEY'],'FolderName:'.$_SERVER['HTTP_DISTRIBUTORAPIKEY']);

$this->CallRestAPI('POST', 'http://192.168.1.55/api/upload' , array('attachment'=> file_get_contents(REPORTPATH . $_SERVER['HTTP_DISTRIBUTORAPIKEY'] . "\\07122017163625.zip")), $headers);


function CallRestAPI($method, $url, $data , $headers) {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

        switch ($method) {
            case "GET":
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "GET");
                break;
            case "POST":
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
                break;
            case "PUT":
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
                break;
            case "DELETE":
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
                break;
        }
        $response = curl_exec($curl);
        echo "Response:".$response;die;

        /* Check for 404 (file not found). */
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        // Check the HTTP Status code
        switch ($httpCode) {
            case 200:
                $error_status = "200: Success";
                return ($data);
                break;
            case 404:
                $error_status = "404: API Not found";
                break;
            case 500:
                $error_status = "500: servers replied with an error.";
                break;
            case 502:
                $error_status = "502: servers may be down or being upgraded. Hopefully they'll be OK soon!";
                break;
            case 503:
                $error_status = "503: service unavailable. Hopefully they'll be OK soon!";
                break;
            default:
                $error_status = "Undocumented error: " . $httpCode . " : " . curl_error($curl);
                break;
        }
        curl_close($curl);
        echo $error_status;
        die;
    }
Hakan SONMEZ
2#
Hakan SONMEZ Reply to 2017-12-08 06:23:41Z

You can get the code from postman on the top right corner. The "code" button. It generates the code any language what you want.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.360881 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO