通过curl库从请求中发送所有文件

时间:2019-06-25 05:36:56

标签: php symfony curl

我需要通过curl库将所有来自symfony请求的文件发送到另一个系统。

我创建了一些类似于旧的php系统的桥梁。如何添加像post变量这样的文件?

实际上,我只需要将文件获取到控制器并通过curl传递相同的$_FILES数组即可。 (以下代码仅是测试。我完全了解安全问题的外观。仅在正确发送文件时才需要帮助)

        $post = '';

        if($request->request->get('complaint')){

            $post = urldecode(http_build_query($request->request->all()));
            $files = array();

            foreach($request->files->all() as $key => $value){
                // How can I add files like a post variable?
                $post .= $myFileInRightFormat;
            }

        }

        $routeName = $request->get('_route');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->getParameter('medos_url').'/edit/'.$id);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, 'b2b='.$this->getParameter('medos_token').'&complaint_id='.$id.'&'.$post);

        $response = curl_exec($ch);

1 个答案:

答案 0 :(得分:0)

以与库的旧版本,多维数组,包含文件的多维数组,CurlFile和http_build_query()相同的方式发送文件存在很多问题。

我相信通过发布代码,我将帮助某些人节省一些时间。我检查了很多解决方案和代码,对我来说仅此一种:

    /**
     * Complaint status list
     * 
     * @Route("/edit/{id}", name="b2b_rma_complain_edit")
     * @Route("/quardian/{id}", name="b2b_rma_complain_quardian_edit")
     * 
     * @return string Twig tpl B2BRMA/Complaint/index.html.twig
     */
    public function editAction(Request $request, $id)
    {

        if($request->request->get('complaint')){

            $post = $request->request->all();

            foreach($request->files->all() as $key => $value){
                if($value){
                    $post[$key] =  new \CurlFile($value->getRealPath(), $value->getMimeType(), $value->getClientOriginalName());
                }
            }

        }

        $post['b2b'] = $this->getParameter('medos_token');
        $post['complaint_id'] = $id;

        $routeName = $request->get('_route');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->getParameter('medos_url').'/edit/'.$id);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->build_post_fields($post));

        $response = curl_exec($ch);

        $crawler = new Crawler($response);
        $html = str_replace('btn-success', 'btn-primary', $crawler->filter('#b2b-curl')->html());

        return $this->render('@B2BRMA/Complaint/edit.html.twig', array('id' => $id, 'routeName' => $routeName, 'html' => preg_replace('~>\s+<~', '><', $html)));
    }

    public function build_post_fields($data, $existingKeys='', &$returnArray=[]){

        if(($data instanceof \CURLFile) or !(is_array($data) or is_object($data))){
            $returnArray[$existingKeys]=$data;
            return $returnArray;
        }
        else{
            foreach ($data as $key => $item) {
                $this->build_post_fields($item,$existingKeys?$existingKeys."[$key]":$key,$returnArray);
            }
            return $returnArray;
        }
    }

当然,这只是示例