提问者:小点点

如何在php中使用curl发布图像


我想改变配置文件图像另一个应用卷曲的要求,我正在尝试下面的代码谁能帮助我请

$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个答案

匿名用户

正如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);
  }