2

Is there an explanation why using array gave me 500 internal server error and using string just works fine with same input ?

    $header_array[] = 'Host: example.com';
    $header_array[] = 'Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3';
 //   $header_array[] = 'Accept-Encoding: gzip, deflate';
    $header_array[] = 'Connection: keep-alive';
    $header_array[] = 'Cache-Control: no-cache';
    $header_array[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
    $header_array[] = 'Pragma: no-cache';
    $header_array[] = 'Referer: https://example.com/post/'.$post_id;
    $header_array[] = 'X-Requested-With: XMLHttpRequest';
    $header_array[] = 'Content-Length: '.strlen("id=".$post_id);
    /* curl */
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $header_array);
    curl_setopt( $ch, CURLOPT_POST, TRUE);

not working (internal server error):

    $post_array['id'] = "1";
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_array);

working:

    $post_string = "id=1";
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_string);

this is also working on another url:

    $post_array['__Token'] = $token;
    $post_array['UserName'] = $this->username;
    $post_array['Password'] = $this->password;
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_array);

curl verbose output of error:

Array
(
    [url] => https://example.com/post/delete
    [content_type] => 
    [http_code] => 500
    [header_size] => 257
    [request_size] => 879
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.370037
    [namelookup_time] => 2.7E-5
    [connect_time] => 0.109487
    [pretransfer_time] => 0.187462
    [size_upload] => 145
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 391
    [download_content_length] => 0
    [upload_content_length] => 145
    [starttransfer_time] => 0.335593
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 188.132.xx.xxx
    [certinfo] => Array
        (
        )

    [primary_port] => 443
    [local_ip] => 172.20.xx.xxx
    [local_port] => 63696
)

my phpinfo states:

Core

PHP Version => 5.5.14
curl

cURL support => enabled
cURL Information => 7.37.1
14
  • 2
    not working is not a usefull problem description Commented Apr 13, 2015 at 23:41
  • @Dagon i've added not working description on my question as 500 internal server error. I hope this is useful for you ? Commented Apr 13, 2015 at 23:48
  • 2
    I think you're supposed to use http_build_query() on the array you want to send with CURLOPT_POSTFIELDS, e.g. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_array)); Commented Apr 14, 2015 at 0:02
  • @scrowler and why it does work without using it on last example ? Commented Apr 14, 2015 at 0:06
  • Well in your last example the $post_string doesn't represent the $post_array values above it so most of that example is irrelevant Commented Apr 14, 2015 at 0:31

1 Answer 1

1

If you take a look at PHP manual, it says:

Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data, while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.

And you have these lines in your code:

$header_array[] = 'Content-Length: '.strlen("id=".$post_id);
$header_array[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';

if you remove those lines, it will be ok!


My code currently looks like this:

<?php 
    //$header_array[] = 'Host: example.com';

    //$header_array[] = 'Accept-Encoding: gzip, deflate';
    $header_array[] = 'Accept-Language: tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3';
    $header_array[] = 'Connection: keep-alive';
    $header_array[] = 'Cache-Control: no-cache';

    $header_array[] = 'Pragma: no-cache';
    $header_array[] = 'Referer: https://example.com/post/'.$post_id;
    $header_array[] = 'X-Requested-With: XMLHttpRequest';
    //$header_array[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
    //$header_array[] = 'Content-Length: '.strlen("id=".$post_id);
    /* curl */

    $ch = curl_init('http://example.com/your/file.php');

    curl_setopt( $ch, CURLOPT_HTTPHEADER, $header_array);
    curl_setopt( $ch, CURLOPT_POST, TRUE);
    $post_array['id'] = "1";
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_array);


    $data = curl_exec($ch);
?>

If it shows the same error, try adding :

$header_array[] = 'Transfer-Encoding: chunked';
Sign up to request clarification or add additional context in comments.

4 Comments

actually i get this error: "HTTP Error 411. The request must be chunked or have a content length." and after adding content-length again it doesn't worked either. Seems like there is no way to use array when it's not "multipart/form-data"
sorry, still same error "HTTP Error 411. The request must be chunked or have a content length."
@motto did you try the last line ?
still no luck. same 500 internal error on this time. i decided to use "http_build_query($post_array)"

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.