【问题标题】:How to send file in PHP 5.05 using cURL如何使用 cURL 在 PHP 5.05 中发送文件
【发布时间】:2019-04-09 18:46:17
【问题描述】:

我正在尝试使用 cURL 在 PHP 5.0.5 中发送文件,但无法做到。它返回布尔(假)。我可以使用命令行发送它,但我需要在我的 PHP 代码中发送它。

这是命令行代码, curl --data-binary @employee_extract.txt https://testserver.com/testapp.aspx --proxy http://111.111.11.11:8080

这是成功返回的消息,

<?xml version="1.0"?>
<Status>
  <result>ok</result>
  <size>192</size>
  <id>20190409134142_C12040C53BCC4AC6B4A09E1BC476D262.txt</id>
  <message>
    <p>file</p>
    <p>No xmldoc posted, saving stream.</p>
  </message>
</Status>

PHP 代码:

$url = 'https://testserver.com/testapp.aspx';
$proxy = '111.111.11.11:8080';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);

// send a file
curl_setopt($ch, CURLOPT_POST, true);
$args['file'] = "@employee_extract.txt;filename=file;type=text/plain"   
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$curl_scraped_page = curl_exec($ch);
curl_close($ch);    
echo $curl_scraped_page;
var_dump($curl_scraped_page);

实际结果:var_dump($curl_scraped_pa​​ge) 返回 bool(false) 预期结果:一条成功的消息

【问题讨论】:

  • "我正在尝试使用 PHP 5.0.5 发送文件" 希望你是在开玩笑。 php.net/eol.php
  • 是的,快 15 岁了。
  • 你有没有使用curl_errno()查看失败的具体原因?
  • 谢谢帕特里克! curl_errno() 帮助我找出问题所在。我最终添加了这个 curl_setopt($rest, CURLOPT_SSL_VERIFYPEER, false);

标签: php curl


【解决方案1】:

您使用的 PHP 版本已于 2005 年终止,大约 15 年前。您的问题可能与此有关。例如,您的示例 URL 使用 HTTPS——您的老式 PHP 解释器中嵌入的 curl 版本可能与它所连接的 Web 服务器的 HTTPS 配置不兼容。

(即使“5.0.5”是一个错字并且您实际上是指 PHP 5.5,该版本也已在 2016 年终止使用。)

将您的系统更新到受支持的 PHP 版本。

【讨论】:

  • 我同意我需要更新我的 PHP 版本。谢谢大家的帮助!
  • 我同意我需要更新我的 PHP 版本。我使用 curl_errno() 来确定具体问题是什么。谢谢大家的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 2015-04-22
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
相关资源
最近更新 更多