1

This is the array of data which i want to post to remote server.

Array
(
    [petTitle] => test
    [petType] => 2
    [breedtype] => dsfsdf
    [dateOfBirth] => 14 Apr 2016
    [postcode] => 390015
    [isPriceNegotiable] => False
    [price] => test
    [isPublished] => 1
    [petQualities] => 2
    [petDescription] => test
    [username] => XXXX@gmail.com
    [file] => Array
        (
            [0] => Array
                (
                    [name] => Desert.jpg
                    [type] => image/jpeg
                    [tmp_name] => D:\xampp\tmp\php1F32.tmp
                    [error] => 0
                    [size] => 845941
                )

            [1] => Array
                (
                    [name] => Koala.jpg
                    [type] => image/jpeg
                    [tmp_name] => D:\xampp\tmp\php1F62.tmp
                    [error] => 0
                    [size] => 780831
                )

        )

)

I am using cURL. However while i send this array with the use of CURLOPT_POSTFIELDS. it generates a notice "PHP Notice: Array to string conversion".

I am using like this : CURLOPT_POSTFIELDS => $data. Please refer the curl request below:

$headers = array("Content-Type:multipart/form-data");
$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_URL => self::url,
    CURLOPT_HEADER => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_ENCODING => "",
    CURLOPT_TIMEOUT => 30,
    CURLOPT_POST => 1,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => $data,
));
$response = curl_exec($curl);

with this request I am able to post the text data. But somehow I am unable to upload the image and add entry in database.

Any help in this will be highly appreciated.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • File uploads doesn't work this way. Read about `CURLOPT_POSTFIELDS` in the [documentation](http://php.net/manual/en/function.curl-setopt.php). It explains how to upload files. – axiac Apr 03 '16 at 06:32
  • Possible duplicate http://stackoverflow.com/questions/5224790/curl-post-format-for-curlopt-postfields – awiebe Apr 03 '16 at 06:34
  • @awiebe It's not a duplicate of that question. The notice is triggered because the array passed to `CURLOPT_POSTFIELDS` is multi-level and it should have only one level (or maybe two, but the values on the second level must not be arrays). But the array is incorrect, in the first place. The poster wants to upload a file and, for that purpose, the value of field `file` doesn't help at all. – axiac Apr 03 '16 at 06:40

1 Answers1

0

The notice is triggered because the array you passed to CURLOPT_POSTFIELDS has incorrect format. The multi-level array you put in file is the cause.

But the array you put in file doesn't help you upload any file. This is how the information about uploaded files looks like on the destination PHP script, but on the source side it looks different.

Replace the value of file with:

$data = array(
    // ... all the text fields here
    'file1' => new CURLFile('... path .../Desert.jpg', 'image/jpeg'),
    'file2' => new CURLFile('... path .../Koala.jpg', 'image/jpeg'),
);

Replace ... path ... in the code above with the paths of the corresponding files.

axiac
  • 68,258
  • 9
  • 99
  • 134
  • Do you have any idea how it will work. I am currently facing an issue. Can you just provide with the code snippet?? – Hiren Prajapati Apr 03 '16 at 08:27
  • How looks the code of the form you are trying to simulate? You should use the same names for the fields. Probably it contains two or more `` and they have different names, aren't they? – axiac Apr 03 '16 at 09:31
  • Yes, I have multiple ``. Their name is img1 to img4. This is because i have taken mutlidimensional array. Because only one image is mandatory and other can be provided or skipped. So if you can help me how exactly i can pass the array which allow to upload thee image on remote server and i can get rid off this **PHP Notice: Array to string conversion** error. I am stuck and This is really very high priority. I have to find the solution anyhow today. Thanks for all your help. – Hiren Prajapati Apr 03 '16 at 10:53