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

问题描述 投票:0回答:1

我想更改其他应用程序卷曲请求的个人资料图片,我正在尝试以下代码,任何人都可以帮我吗

$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);
  }
php curl zend-framework socialengine
1个回答
0
投票

正如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);
  }
© www.soinside.com 2019 - 2024. All rights reserved.