我怎样才能上传文件Curl PHP

时间:2018-05-10 18:39:52

标签: php curl

我正在尝试使用Curl和PHP发布文件。

我按照说明操作:

提交文件

使用带有encType:multipart / form-data的HTTPs POST提交上传文件(用于存款或批量查询)。所有提交的URL都是https:// doi.crossref.org/servlet/deposit。您也可以使用https://test.crossref.org/servlet/deposit将提交内容发布到我们的测试系统。

使用curl的例子:

上传文件:

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

这是我正在尝试的代码

<?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;
    }

&GT;

这是错误:

HTTP / 1.1 405方法不允许服务器:Apache-Coyote / 1.1 Crossref-Deployment-Name:cs3-1 Content-Type:text / plain; charset = UTF-8 Content-Language:en-US Content-Length: 17日期:2018年5月10日星期四17:18:10 GMT连接:关闭Strict-Transport-Security:max-age = 15768000 GET不支持

任何想法我怎么能把这个文件?

修改

我按照@waterloomatt的说明进行操作,现在又出现了另一个错误

这是代码:

<!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; 
}

错误消息:

HTTP / 1.1 100继续HTTP / 1.1 401未经授权的服务器: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:Monday,November May 14:13:07 GMT Connection:close Strict-Transport-Security: max-age = 15768000请求中没有登录信息

有谁知道为什么登录名和密码无法被识别?

2 个答案:

答案 0 :(得分:0)

我有同样的问题。我做了什么:

首先,将URL作为参数添加到 curl_init

    $ch = curl_init($url);

第二,使用类CURLFile代替 @

最后,像这样发送凭据,而不是CURLOPT_USERPWD。

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

答案 1 :(得分:-1)

如果有帮助,请使用本文。 Upload files with CURL