【问题标题】:Using PUT method with PHP cUrl Library在 PHP cUrl 库中使用 PUT 方法
【发布时间】:2011-04-26 20:56:00
【问题描述】:

我能够成功运行以下 curl 命令(在命令行中):

curl -XPOST --basic -u user:password -H accept:application/json -H Content-type:application/json --data-binary '{ "@queryid" : 1234 }' http://localhost/rest/run?10

这是我到目前为止所做的,但它似乎不适用于我正在使用的 REST 服务:

$headers = array(
    'Accept: application/json',
    'Content-Type: application/json',
);

$url = 'http://localhost/rest/run?10';
$query = '{ "@queryid" : 1234 }';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "user:password");

curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
curl_setopt($ch, CURLOPT_POSTFIELDSIZE, strlen($query));

$output = curl_exec($ch);

echo $output;

尝试使用 PUT 方法转换 --data-binary 时的正确方法是什么?

【问题讨论】:

    标签: php curl put


    【解决方案1】:

    您可以使用php://temp,而不是在磁盘上创建临时文件。

    $body = 'the RAW data string I want to send';
    
    /** use a max of 256KB of RAM before going to disk */
    $fp = fopen('php://temp/maxmemory:256000', 'w');
    
    if (!$fp) 
    {
        die('could not open temp memory data');
    }
    
    fwrite($fp, $body);
    fseek($fp, 0); 
    
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_INFILE, $fp); // file pointer
    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));                            
    

    好处是没有磁盘 IO,因此它应该更快,服务器上的负载更少。

    【讨论】:

    • 这太棒了!我不知道临时 FD,它帮助我使用 curl 为 youtube 加载恢复上传(将剩余字节读入内存,然后使用您的方法正常上传)
    • 模式不应该是'w+'吗?
    【解决方案2】:

    大家好,我使用这个配置让它工作了:

    // Start curl
    $ch = curl_init();
    // URL for curl
    $url = "http://localhost/";
    
    // Clean up string
    $putString = stripslashes($query);
    // Put string into a temporary file
    $putData = tmpfile();
    // Write the string to the temporary file
    fwrite($putData, $putString);
    // Move back to the beginning of the file
    fseek($putData, 0);
    
    // Headers
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    // Binary transfer i.e. --data-BINARY
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    // Using a PUT method i.e. -XPUT
    curl_setopt($ch, CURLOPT_PUT, true);
    // Instead of POST fields use these settings
    curl_setopt($ch, CURLOPT_INFILE, $putData);
    curl_setopt($ch, CURLOPT_INFILESIZE, strlen($putString));
    
    $output = curl_exec($ch);
    echo $output;
    
    // Close the file
    fclose($putData);
    // Stop curl
    curl_close($ch);
    

    :)

    【讨论】:

    • +1 感谢您抽出宝贵时间回来回答您自己的问题。您为我节省了很多时间
    【解决方案3】:

    所有需要设置的只是重用post方法的自定义请求。

    CURLOPT_URL=>$url,
    CURLOPT_CUSTOMREQUEST=>'PUT',
    CURLOPT_POSTFIELDS=>$params,
    

    【讨论】:

    • 在访问请求中没有传输实际数据的 REST API 时,此指针对我很有帮助(url 包含参数)
    • 这种方法使通过 PUT 与 rest 交谈变得更加容易。没有文件刺痛,只需使用 POST 基础设施。
    猜你喜欢
    • 2019-09-13
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多