Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么使用带有CURLOPT_POSTFIELDS的一项数组不起作用?_Php_Curl - Fatal编程技术网

Php 为什么使用带有CURLOPT_POSTFIELDS的一项数组不起作用?

Php 为什么使用带有CURLOPT_POSTFIELDS的一项数组不起作用?,php,curl,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[] =

有没有一个解释,为什么使用数组会给我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_string = "id=1";
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_string);
这也适用于另一个url:

    $post_array['__Token'] = $token;
    $post_array['UserName'] = $this->username;
    $post_array['Password'] = $this->password;
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_array);
错误的curl详细输出:

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

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

将数组传递给CURLOPT_POSTFIELDS会将数据编码为 传递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);
?>

不工作不是一个有用的问题description@Dagon我已经在我的问题上添加了不工作的描述,作为500内部服务器错误。我希望这对你有用?我想你应该在你想用
CURLOPT_POSTFIELDS
发送的数组上使用,例如
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($post_array))
@scrowler以及为什么它不在上一个示例中使用它就可以工作?在上一个示例中,
$post\u字符串
并不表示它上面的
$post\u数组
值,因此该示例中的大多数都是不相关的,我得到以下错误:“HTTP错误411。请求必须分块或具有内容长度。”在再次添加内容长度后,它也不起作用。似乎在数组不是“多部分/表单数据”的情况下无法使用数组抱歉,仍然是同一个错误“HTTP错误411。请求必须分块或具有内容长度。”@monit您尝试了最后一行了吗?仍然没有成功。这一次同样的500内部错误。我决定使用“http\u build\u query($post\u数组)”
<?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';