PHP - Upload an image file to other domain with CURL

后端 未结 4 1380
孤城傲影
孤城傲影 2020-12-10 08:09

I have two domain, as example site1.loc and site2.loc. In site1.loc i have a php form file like this:

<         


        
4条回答
  •  孤街浪徒
    2020-12-10 08:46

    CURLOPT_POSTFIELDS The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like 'para1=va1¶2=val2&...' or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

    http://php.net/manual/en/function.curl-setopt.php

    Code snippet:

    $postvars['file'] = '@full/path/to/file.jpg';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, count($fields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
    curl_exec($ch);
    

提交回复
热议问题