简体   繁体   中英

How can I upload file Curl PHP

I'm trying to post a file with Curl and PHP.

I have following the instructions:

Submitting files

Uploading files (for deposit or for bulk queries) are submitted using HTTPs POST with the encType: multipart/form-data. The URL for all submissions is https:// doi.crossref.org/servlet/deposit. You may also POST submissions to our test system using https://test.crossref.org/servlet/deposit .

Examples using curl:

To upload a file:

curl -F 'operation=doQueryUpload' -F 'login_id=USERNAME' -F 'login_passwd=PASSWORD' -F 'fname=@FILENAME' https:// doi.crossref.org/servlet/deposit

This is the code I am trying

<?php
ini_set('display_errors', E_ALL);
define('CROSSREF_API_URL', 'https://test.crossref.org/servlet/deposit');
define('CROSSREF_API_DEPOSIT_OK', 303);
define('CROSSREF_API_RESPONSE_OK', 200);

echo registerDoi(realpath('test.xml'));



function registerDoi($filename) {


        $curlCh = curl_init();

        curl_setopt($curlCh, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curlCh, CURLOPT_POST, true);
        curl_setopt($curlCh, CURLOPT_HEADER, 1);
        curl_setopt($curlCh, CURLOPT_BINARYTRANSFER, true);

        $username = 'username';
        $password = '123456';

        curl_setopt($curlCh, CURLOPT_URL, CROSSREF_API_URL);
        curl_setopt($curlCh, CURLOPT_USERPWD, "$username:$password");

        // Transmit XML data.
        assert(is_readable($filename));
        $fh = fopen($filename, 'rb');



        $httpheaders = array();
        $httpheaders[] = 'Content-Type: application/vnd.crossref.deposit+xml';
        $httpheaders[] = 'Content-Length: ' . filesize($filename);

        curl_setopt($curlCh, CURLOPT_HTTPHEADER, $httpheaders);
        curl_setopt($curlCh, CURLOPT_INFILE, $fh);
        curl_setopt($curlCh, CURLOPT_INFILESIZE, filesize($filename));



        $response = curl_exec($curlCh);



        if ($response === false) {
            $result = 'response from server.';
        } elseif ( $status = curl_getinfo($curlCh, CURLINFO_HTTP_CODE) != CROSSREF_API_DEPOSIT_OK ) {
            $result = $response;
        } else {
            // Deposit was received
            $result = 'recebido';

        }

        curl_close($curlCh);
        return $result;
    }

?>

This is the error:

HTTP/1.1 405 Method Not Allowed Server: Apache-Coyote/1.1 Crossref-Deployment-Name: cs3-1 Content-Type: text/plain;charset=UTF-8 Content-Language: en-US Content-Length: 17 Date: Thu, 10 May 2018 17:18:10 GMT Connection: close Strict-Transport-Security: max-age=15768000 GET not supported

Any idea how can I PUT this file?


EDIT

I followed the instructions of @waterloomatt and now I get another error

This is the code:

<!DOCTYPE html>
<html>
<body>

<form action="deposito.php" method="post" name="frmUpload" enctype="multipart/form-data">
<tr>
  <td>Upload</td>
  <td align="center">:</td>
  <td><input name="file" type="file" id="file"/></td>
</tr>
<tr>
  <td>&nbsp;</td>
  <td align="center">&nbsp;</td>
  <td><input name="btnUpload" type="submit" value="Upload" /></td>
</tr>

</body>
</html>





<?php


if (isset($_POST['btnUpload']))
{


$url = "https://test.crossref.org/servlet/deposit"; 
$filename = $_FILES['file']['name'];
$filedata = $_FILES['file']['tmp_name'];
$filesize = $_FILES['file']['size'];

$username = 'usename';
$password = '1234';

if ($filedata != '')
{



    $headers = array("Content-Type:multipart/form-data"); // cURL headers for file uploading
    $postfields = array("filedata" => "@$filedata", "fname" => $filename);
    $ch = curl_init();
    $options = array(
        CURLOPT_URL => $url,
        CURLOPT_HEADER => true,
        CURLOPT_POST => 1,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_POSTFIELDS => $postfields,
        CURLOPT_INFILESIZE => $filesize,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_USERPWD => "$username:$password",
        CURLOPT_BINARYTRANSFER => true
    ); // cURL options
    curl_setopt_array($ch, $options);
    $errmsg = curl_exec($ch);

    curl_close($ch);


}
else
{
    $errmsg = "Please select the file";
}

echo $errmsg; 
}

error message:

HTTP/1.1 100 Continue HTTP/1.1 401 Unauthorized Server: Apache-Coyote/1.1 Crossref-Deployment-Name: cs3-1 Set-Cookie: JSESSIONID=B2110CC01E3542D50E21D9898D4FD5F2; Path=/ Content-Type: text/plain;charset=UTF-8 Content-Language: en-US Content-Length: 24 Date: Mon, 14 May 2018 13:10:07 GMT Connection: close Strict-Transport-Security: max-age=15768000 No login info in request

Does anyone know why login and password are not being recognized?

I had the same problem. What I did:

First, add the URL as param to curl_init

    $ch = curl_init($url);

Second, use the class CURLFile instead of @ .

Finally, instead of CURLOPT_USERPWD send the credentials like this.

    $postfields = array(
      'login_id' => $username,
      'login_passwd' => $password,
      'fname' => new CURLFile($filedata),
    );

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