简体   繁体   English

PHP cURL中的换行符

[英]Line Breaks in PHP cURL

I'm using the following command to update a database (the line breaks are vital): 我正在使用以下命令来更新数据库(换行至关重要):

curl -X PUT -H 'Content-Type: multipart/form-data; boundary=myboundary' -d '--myboundary Content-ID: <request>

{"jsonKey":"jsonValue"} --myboundary-' 'http://target.url.com/path/to/folder'

This works correctly, but the PHP equivalent has problems: 这可以正常工作,但是等效的PHP存在问题:

$data = '--myboundary Content-ID: <request>

{"jsonKey":"jsonValue"} --myboundary-';

    $handle = curl_init();
    curl_setopt_array(
        $handle, 
        array(
            CURLOPT_URL => 'http://target.url.com/path/to/folder',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CUSTOMREQUEST => 'PUT',
            CURLOPT_HTTPHEADER => 'Content-Type: multipart/form-data; boundary=myboundary',
            CURLOPT_BINARYTRANSFER => true,
            CURLOPT_POSTFIELDS => $data
         )
    );
    $response = curl_exec($handle);
    if($response === false){
        echo 'curl error: ' . curl_error($handle);
    }
    curl_close($handle);
    return $response;

It returns no error, but the data entered as json is set to null. 它不返回错误,但作为json输入的数据设置为null。 I've checked everything else, but can't seem to find clear information on how cURL processes line breaks, and assume there is some difference between PHP breaks and console breaks. 我检查了所有其他内容,但似乎找不到关于cURL如何处理换行符的清晰信息,并假定PHP换行符和控制台换行符之间存在一些差异。

Any insight into the issue of line breaks, or why this may not be working would be appreciated. 如果您对换行问题有任何见解,或者为什么这样做不起作用,将不胜感激。

And in case anyone is thinking the obvious: no, I can't alter the database, or even view it (work regulations), and this needs to run from PHP (same reason). 万一有人想到这很明显:不,我不能更改数据库,甚至不能查看数据库(工作规定),而这需要从PHP运行(同样的原因)。

In the first example, you're using POST. 在第一个示例中,您正在使用POST。 In the second example, you're using PUT. 在第二个示例中,您正在使用PUT。 Are you sure that's right? 你确定是对的吗?

            CURLOPT_CUSTOMREQUEST => 'PUT',

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

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