Как разместить изображение с помощью curl в php - PullRequest
0 голосов
/ 16 июня 2020

Я хочу изменить изображение профиля. Другой запрос на завиток приложения. Я пытаюсь выполнить приведенный ниже код. Кто-нибудь может мне помочь, пожалуйста

$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);
  }
.

1 Ответ

1 голос
/ 16 июня 2020

Как сказал Anoxy, вам нужно поместить в заголовок 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);
  }
...