简体   繁体   中英

How to post image using curl in php

I want to change profile image another application curl request i am trying below code can anyone help me please

$viewer = Engine_Api::_()->user()->getViewer();

$apiData = array(
  "email" => $viewer->email,
  "profile_image_file" => $_FILES['Filedata']['name'],
);
$apiHost = "https://tenant.thetenantsnet.co.uk/api/api/save_profile_image";

$response = $this->callRiseAPI2($apiData,$apiHost);

   private function callRiseAPI2($apiData,$apiHost){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $apiHost);
        curl_setopt($ch, CURLOPT_POST, count($apiData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $jsonData = curl_exec($ch);
        if (false === $jsonData) {
            throw new \Exception("Error: _makeOAuthCall() - cURL error: " . curl_error($ch));
        }
        curl_close($ch);

        //return the API response 
        return json_decode($jsonData);
  }

As Anoxy said, you need to put in the header the Content-Type:

$viewer = Engine_Api::_()->user()->getViewer();

$apiData = array(
  "email" => $viewer->email,
  "profile_image_file" => $_FILES['Filedata']['name'],
);
$apiHost = "https://tenant.thetenantsnet.co.uk/api/api/save_profile_image";

$response = $this->callRiseAPI2($apiData,$apiHost);

   private function callRiseAPI2($apiData,$apiHost){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $apiHost);
        curl_setopt($ch, CURLOPT_POST, count($apiData));
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-Type: multipart/form-data');

        $jsonData = curl_exec($ch);
        if (false === $jsonData) {
            throw new \Exception("Error: _makeOAuthCall() - cURL error: " . curl_error($ch));
        }
        curl_close($ch);

        //return the API response 
        return json_decode($jsonData);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM