简体   繁体   English

如何上传Curl PHP文件

[英]How can I upload file Curl PHP

I'm trying to post a file with Curl and PHP. 我正在尝试使用Curl和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. 使用HTTP POST(具有encType:multipart / form-data)提交上载文件(用于存款或用于批量查询)。 The URL for all submissions is https:// doi.crossref.org/servlet/deposit. 所有提交的URL为https:// doi.crossref.org/servlet/deposit。 You may also POST submissions to our test system using https://test.crossref.org/servlet/deposit . 您也可以使用https://test.crossref.org/servlet/deposit将提交内容提交到我们的测试系统。

Examples using curl: 使用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 curl -F'操作= doQueryUpload'-F'login_id =用户名'-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 HTTP / 1.1 405方法不允许服务器:Apache-Coyote / 1.1 Crossref-Deployment-Name:cs3-1内容类型:text / plain; charset = UTF-8内容语言:zh-CN内容长度:17日期: 2018年5月10日,星期四,格林尼治标准时间(GMT)连接:关闭严格运输安全:max-age = 15768000不支持GET

Any idea how can I PUT this file? 知道如何放置该文件吗?


EDIT 编辑

I followed the instructions of @waterloomatt and now I get another error 我按照@waterloomatt的指示进行操作,现在又遇到另一个错误

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; 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: Mon, 14 May 2018 13:10:07 GMT Connection: close Strict-Transport-Security: max-age=15768000 No login info in request 路径= /内容类型:文本/纯文本;字符集= UTF-8内容语言:zh-CN内容长度:24日期:2018年5月14日星期一13:10:07 GMT连接:关闭严格的传输安全性: max-age = 15768000请求中没有登录信息

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 首先,将URL作为参数添加到curl_init

    $ch = curl_init($url);

Second, use the class CURLFile instead of @ . 其次,使用类CURLFile代替@

Finally, instead of CURLOPT_USERPWD send the credentials like this. 最后,代替CURLOPT_USERPWD发送这样的凭据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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