2

I have looked through other answers but I believe that my problem may be different. I'm not a cURL whiz and I'm hoping that someone can advise me where I should be directing my efforts.

Local PHP script transmits $_POST array containing text values and a file. Data is received by a PHP script on server. No browser in the equation. I picked up the code off the internet.

Works perfectly on local development HTTP server (Cherokee). Fails on remote live HTTP server (Apache).

Sending code:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    post_array=array("my_file"=>"@".$File,"upload"=>"Upload","var1"=>P1,"var2"=>P2,"var3"=>P3);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_array);
if(!curl_errno($ch))
{
 $info = curl_getinfo($ch);

 echo 'Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}

Sending script displays $_POST content as:

Array
(
    [my_file] => @<path to file>
    [upload] => Upload
    [var1] => P1
    [var2] => P2
    [var3] => P3
)

On remote server, $_POST array is received by script as:

Array
(
    [upload] => Upload
)

I'm using CURLOPT_VERBOSE and I get the following screen output:

* About to connect() to <domain name> port 80 (#0)
*   Trying 82.71.205.4... * connected
> POST <receiving file> HTTP/1.1
User-Agent: Mozilla/4.0 (compatible;)
Host: <domain name>
Accept: */*
Content-Length: 22091
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------734a2c311f30

< HTTP/1.1 100 Continue
< HTTP/1.1 200 OK
< Date: Thu, 21 Nov 2013 14:09:13 GMT
< Server: Apache
< X-Powered-By: PHP/5.3.17
< Content-Length: 199
< Content-Type: text/html
< 
* Connection #0 to host <domain name> left intact

Took 0.718261 seconds to send a request to <receiving file>

So, I know the following:

1/ Script works perfectly on dev server, but on live server...
2/ curl_errno says no errors.
3/ CURLOPT_VERBOSE reports expected array size (22091).
4/ Receiving file receives data, but not $_POST array content.
5/ No entries in server error log.

Can anyone tell me where I should be investigating please?

4 Answers 4

1

This two lines need to be reversed:

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
post_array=array("my_file"=>"@".$File,"upload"=>"Upload","var1"=>P1,"var2"=>P2,"var3"=>P3);

Plus, post_array = ... is invalid. You're assigning to a constant.

It should be:

$post = array( blah blah blah );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
Sign up to request clarification or add additional context in comments.

1 Comment

Swapped the order. No difference. Also, post_array = was a typo. My bad.
0

Because of application/x-www-form-urlencoded (by default) try http_build_query():

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_array));

2 Comments

Why? cURL can accept an array directly and do the query-building itself. So http_build_query() isn't required.
Interestingly, that restored the @file to the array but not the vars. I would have expected it to work or not. How odd.
0

This isn't really a proper answer but I found that if I only upload the file and do not include any other values in the $_POST array then it works on Apache.

I've not been able to find out why this works on Cherokee and not Apache but at least I have a workaround. Moving to a web host that uses Cherokee is going to be more trouble than it's worth.

I'll consider this a workaround. Perhaps it may help someone in future.

Comments

0

I believe the issue is related to having the "at" symbol (@) at the beginning of a POST data field. I have the same problem in my script. I believe urlencoding the data should fix the problem.

post_array=array("my_file"=>urlencode("@".$File),"upload"=>"Upload","var1"=>P1,"var2"=>P2,"var3"=>P3);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.