Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 上载带有cURL的文件时,会使用头信息修改文件_Php_Curl_Content Type_Zendesk_Zendesk Api - Fatal编程技术网

Php 上载带有cURL的文件时,会使用头信息修改文件

Php 上载带有cURL的文件时,会使用头信息修改文件,php,curl,content-type,zendesk,zendesk-api,Php,Curl,Content Type,Zendesk,Zendesk Api,当使用cURL上传文件时,我发现远程端的文件被修改,头信息被转储到文件中。我怀疑这是在我的标题信息,但我失去了超出这一点。我几乎什么都在试 这是我正在使用的代码 $fLocation = realpath('file.csv'); $finfo = finfo_open(FILEINFO_MIME_TYPE); $fType = finfo_file($finfo, $fLocation); finfo_close($finfo); $cFile = curl_file_

当使用cURL上传文件时,我发现远程端的文件被修改,头信息被转储到文件中。我怀疑这是在我的标题信息,但我失去了超出这一点。我几乎什么都在试

这是我正在使用的代码

$fLocation = realpath('file.csv');  
$finfo = finfo_open(FILEINFO_MIME_TYPE);  
$fType = finfo_file($finfo, $fLocation);  
finfo_close($finfo);      
$cFile = curl_file_create($fLocation);  
$post = array('file'=> $cFile);  
$ch = curl_init();  
curl_setopt($ch, CURLOPT_URL, $url);  
curl_setopt($ch, CURLOPT_POST,1);  
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);  
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);  
curl_setopt($ch, CURLOPT_HTTPHEADER, "Content-Type: application/octet-stream");  
$result = curl_exec($ch);  
我尝试了几种不同的标题内容类型,从应用程序/二进制到多部分/表单数据,甚至在标题中根本不发送内容类型。我想我需要在标题中添加更多内容

无论如何,我发送的文件是一个.csv文件,但我想在最后发送一个.xlsx文件(我正在使用.csv进行测试)。当我从远程服务器获取文件,并在文本编辑器中打开它时,我看到文件的开头和结尾添加了信息,文件的原始数据介于两者之间。下面是添加到顶部的内容

=--------------------------a0fe4530540c659b  
Content-Disposition: form-data; name="file"; filename="/home/username/website/path/file.csv"  
Content-Type: application/octet-stream 
还有,底部

--------------------------a0fe4530540c659b--
基本上,当我的所有.xlsx文件到达另一端时,我发现了这一点。我将发送的内容更改为纯文本文件(CSV),这时我才意识到发生了什么

这里有更有趣的信息。实际上,我是通过ZenDesk的API向ZenDesk发送一些东西,很明显,我是在PHP中使用cURL实现这一点的。当我从Zendesk得到JSON回复时,部分内容如下所示

[file_name] => file.csv  
[content_url] => https://org.zendesk.com/attachments/token/jdjdjdjdjdjhdjdjdj/?name=file.csv  
[content_type] => multipart/form-data  
[size] => 5615  
有趣的是,它看到了我上传的多部分/表单数据。即使我更改了标题中的内容类型,这仍然是正确的


当然,我在这里遗漏了一些愚蠢而简单的东西。

上传文件时不需要设置
内容类型:application/octet-stream
或其他标题。只需要使用方法发送您的文件

您的代码必须是这样的:

if (function_exists('curl_file_create')) { // for php 5.5+
    $File_Address = curl_file_create($file_name_with_full_path);
} else {
    $File_Address = '@' . realpath($file_name_with_full_path);
}

$post = array('file_name'=> $File_Address);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);

$result = curl_exec($ch);

上传文件不需要设置
内容类型:application/octet stream
或其他标题。只需要使用方法发送您的文件

您的代码必须是这样的:

if (function_exists('curl_file_create')) { // for php 5.5+
    $File_Address = curl_file_create($file_name_with_full_path);
} else {
    $File_Address = '@' . realpath($file_name_with_full_path);
}

$post = array('file_name'=> $File_Address);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);

$result = curl_exec($ch);

大家都知道,这和标题有关。这是最终为我解决问题的代码

$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, $json_url);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/binary'));
curl_setopt($ch, CURLOPT_POST, true);
$file = fopen($fLocation, 'r');
$size = filesize($fLocation);
$fildata = fread($file,$size);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fildata);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);

大家都知道,这和标题有关。这是最终为我解决问题的代码

$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10 );
curl_setopt($ch, CURLOPT_URL, $json_url);
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/binary'));
curl_setopt($ch, CURLOPT_POST, true);
$file = fopen($fLocation, 'r');
$size = filesize($fLocation);
$fildata = fread($file,$size);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fildata);
curl_setopt($ch, CURLOPT_INFILE, $file);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$result = curl_exec($ch);

谢谢你的回复,但是没有问题。我不再得到“=----------------------------a0fe4530540c659b”,但我仍然得到CSV文件中的内容。我想肯定还有别的事suppose@PaulMichael上传后你能告诉我你的文件里有什么吗?我想你的源文件可能有问题!我的源文件绝对不是问题所在。它很干净。在我上传文件之前,它有30行信息,2列。上传后,30行被向下推,前2行被内容配置填充:表单数据;name=“file”;filename=“/home/username/website/path/file.csv”内容类型:application/octet stream------因此,另一端的文件正在流中的某个地方被损坏。远端接收我的全部请求并将其作为文件。我只需要它接受文件,不包括标题。谢谢你的答复,但没有bueno。我不再得到“=----------------------------a0fe4530540c659b”,但我仍然得到CSV文件中的内容。我想肯定还有别的事suppose@PaulMichael上传后你能告诉我你的文件里有什么吗?我想你的源文件可能有问题!我的源文件绝对不是问题所在。它很干净。在我上传文件之前,它有30行信息,2列。上传后,30行被向下推,前2行被内容配置填充:表单数据;name=“file”;filename=“/home/username/website/path/file.csv”内容类型:application/octet stream------因此,另一端的文件正在流中的某个地方被损坏。远端接收我的全部请求并将其作为文件。我只需要它来接受文件,而不包括标题。大家都知道,这与标题有关。下面的代码最终为我解决了这个问题。顺便说一句,你实际上是在撒谎。当您给CURLOPT_POSTFIELDS一个数组时,curl将以
Multipart/form data
格式上载文件,其中正确的头看起来像
Content Type:Multipart/form data;boundary=---------------------------fe1569598bc62ae7
-但是您发送了
内容类型:application/octet stream
,告诉服务器您发送了文件raw(和raw!=多部分/表单数据),大家都知道,这与标题有关。下面的代码最终为我解决了这个问题。顺便说一句,你实际上是在撒谎。当您给CURLOPT_POSTFIELDS一个数组时,curl将以
Multipart/form data
格式上载文件,其中正确的头看起来像
Content Type:Multipart/form data;boundary=---------------------------fe1569598bc62ae7
-但是您发送了
内容类型:应用程序/octet流
,告诉服务器您发送了文件raw(和raw!=多部分/表单数据)警告,如果上载程序在Windows上运行,此代码将弄乱二进制数据。要获得在Linux和Windows上都能工作的安全可移植行为,请更改
$file=fopen($fLocation,'r')
$file=fopen($fLocation,'rb')警告,如果上载程序在Windows上运行,此代码将弄乱二进制数据。要获得在Linux和Windows上都能工作的安全可移植行为,请更改
$file=fopen($fLocation,'r')
$file=fopen($fLocation,'rb')