简体   繁体   中英

cli curl to php curl for uploading file

I am trying to set up the php curl library call to take:

curl --location \
     --header "authorization: LOW $accesskey:$secret" \
     --upload-file /home/samuel/public_html/intro-to-k.pdf \
     http://s3.us.archive.org/sam-s3-test-08/demo-intro-to-k.pdf

(this is for Internet archive's api: http://archive.org/help/abouts3.txt )

This is currently on a Windows 7 dev box, but will be moving to Ubuntu

I have tried:

        $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('authorization: LOW XXXX:XXXXX'));
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_URL, 'http://s3.us.archive.org/a_tested_working_dir/the_file_ane_and_ext' );
    $post_array = array(
        "upload-file"=>file_get_contents($absolute_path_to_my_file)
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
    $response = curl_exec($ch);
    echo $response;

Error: MalformedPOSTRequestThe body of your POST request is not well-formed multipart/form-data.bucket must be in dns hostnameXXXXXXXXXX

I'm able to substitute my own values into the cli curl statement & upload without issue; I just can't seem to get the php curl correct

TIA!


A --libcurl dump produced:

CURLcode ret;
CURL *hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, (curl_off_t)64d);
curl_easy_setopt(hnd, CURLOPT_URL, "http://s3.us.archive.org/jeffs_test_1301_librivox/test-francesbaird3.mp3");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1);
curl_easy_setopt(hnd, CURLOPT_UPLOAD, 1);
curl_easy_setopt(hnd, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3");
curl_easy_setopt(hnd, CURLOPT_CAINFO, "C:\Program Files\Git\bin\curl-ca-bundle.crt");
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 1);
curl_easy_setopt(hnd, CURLOPT_SSH_KNOWNHOSTS, "c:/Users/JMadsen/_ssh/known_hosts");
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50);
ret = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);

I think it might be ssl verify, but just tried setting to false & no change

The post field initialization for uploading file is wrong. You dont need to pass contents of file. Just pass the path of the file with @ prefix.

$post_array = array(
    "upload-file"=>'@'. $absolute_path_to_my_file
);

According to the --libcurl dump add the following options too.

curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "curl/7.21.1 (i686-pc-mingw32) libcurl/7.21.1 OpenSSL/0.9.8r zlib/1.2.3");
curl_setopt($ch, CURLOPT_CAINFO, "C:\Program Files\Git\bin\curl-ca-bundle.crt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSH_KNOWNHOSTS, "c:/Users/JMadsen/_ssh/known_hosts");
curl_setopt($ch, CURLOPT_MAXREDIRS, 50);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM