0

I am trying to send file via curl, but keep getting this error:

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

This is my code at the moment:

    $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;

How can I make this working?

1
  • First of all you should copy / paste also line numbers, or highlight line 26, so we can take a better guess at the error. Commented Apr 13, 2016 at 16:22

2 Answers 2

1

You can't have an array as a value, it's trying to convert the post data into a string. Check what the website you are posting to is expecting for "recipients", my guess would be some form of JSON string.

Sign up to request clarification or add additional context in comments.

Comments

0

When you pass an array to CURLOPT_POSTFIELDS it will try to build a form-data content (see http://php.net/manual/en/function.curl-setopt.php for details).

But you have nested arrays here:

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

and this cannot work. You'll have to find a different way to pass multiple values as recipients, either as a JSON string as @fire mentioned, or with a serialized value, or whatever transformation you prefer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.