PHP - 使用https通过curl发送文件

时间:2016-04-13 16:19:19

标签: php curl

我正在尝试通过curl发送文件,但不断收到此错误:

PHP Notice:  Array to string conversion in /home/sasha/Documents/OffProjects/test/index.php on line 26

这是我目前的代码:

    $target_url = 'https://address';
    $file_name_with_full_path = realpath('mpthreetest.mp3');

      $post = [
          'textNote'     => 'This is test',
          'audioFile'    => '@'.$file_name_with_full_path,
          'department'   => 'Test',
          'timeDetected' => round(microtime(true) * 1000),
          'subjectLine'  => 'Test Test',
          'recipients'   => ['phone-number' => '111111111']
          ];

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL,$target_url);
      curl_setopt($ch, CURLOPT_PORT, 443);
      curl_setopt($ch, CURLOPT_POST,1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
      curl_setopt($ch,CURLOPT_FOLLOWLOCATION, true);

      $result=curl_exec ($ch);
      curl_close ($ch);

  echo $result;

我怎样才能使这个工作?

2 个答案:

答案 0 :(得分:1)

您不能将数组作为值,而是尝试将后置数据转换为字符串。检查您要发布的网站对于#34;收件人的期望",我的猜测是某种形式的JSON字符串。

答案 1 :(得分:0)

当您将数组传递给CURLOPT_POSTFIELDS时,它会尝试构建form-data内容(有关详细信息,请参阅http://php.net/manual/en/function.curl-setopt.php)。

但是你有嵌套数组:

....
'recipients'   => ['phone-number' => '111111111']
...

这不起作用。您必须找到另一种方法将多个值作为recipients传递,或者作为@fire引用的JSON字符串,或者使用序列化值,或者您喜欢的任何转换。