为什么使用CURLOPT_POSTFIELDS的一个项目数组不起作用?

时间:2015-04-13 23:33:02

标签: php curl

有没有解释为什么使用数组给我500内部服务器错误并且使用字符串只能在相同的输入下正常工作?

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

无法正常工作(内部服务器错误):

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

工作:

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

这也适用于另一个网址:

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

curl verbose错误输出:

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
)

我的phpinfo声明:

Core

PHP Version => 5.5.14
curl

cURL support => enabled
cURL Information => 7.37.1

1 个答案:

答案 0 :(得分:1)

如果您查看PHP手册,它会说:

  

将数组传递给CURLOPT_POSTFIELDS会将数据编码为   multipart / form-data,传递URL编码的字符串将进行编码   数据为application / x-www-form-urlencoded。

你的代码中有这些行:

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

如果删除这些行,就可以了!


我的代码目前看起来像这样:

<?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);
?>

如果显示相同的错误,请尝试添加:

$header_array[] = 'Transfer-Encoding: chunked';