1. <i id='0Ryqk'><tr id='0Ryqk'><dt id='0Ryqk'><q id='0Ryqk'><span id='0Ryqk'><b id='0Ryqk'><form id='0Ryqk'><ins id='0Ryqk'></ins><ul id='0Ryqk'></ul><sub id='0Ryqk'></sub></form><legend id='0Ryqk'></legend><bdo id='0Ryqk'><pre id='0Ryqk'><center id='0Ryqk'></center></pre></bdo></b><th id='0Ryqk'></th></span></q></dt></tr></i><div id='0Ryqk'><tfoot id='0Ryqk'></tfoot><dl id='0Ryqk'><fieldset id='0Ryqk'></fieldset></dl></div>
        <bdo id='0Ryqk'></bdo><ul id='0Ryqk'></ul>

      1. <tfoot id='0Ryqk'></tfoot>

        <small id='0Ryqk'></small><noframes id='0Ryqk'>

        <legend id='0Ryqk'><style id='0Ryqk'><dir id='0Ryqk'><q id='0Ryqk'></q></dir></style></legend>

        PHP cURL Content-Length 和 Content-Type 错误

        PHP cURL Content-Length and Content-Type wrong(PHP cURL Content-Length 和 Content-Type 错误)

          • <bdo id='TTutH'></bdo><ul id='TTutH'></ul>
              <tbody id='TTutH'></tbody>
          • <tfoot id='TTutH'></tfoot>

          • <small id='TTutH'></small><noframes id='TTutH'>

                <legend id='TTutH'><style id='TTutH'><dir id='TTutH'><q id='TTutH'></q></dir></style></legend>
                  <i id='TTutH'><tr id='TTutH'><dt id='TTutH'><q id='TTutH'><span id='TTutH'><b id='TTutH'><form id='TTutH'><ins id='TTutH'></ins><ul id='TTutH'></ul><sub id='TTutH'></sub></form><legend id='TTutH'></legend><bdo id='TTutH'><pre id='TTutH'><center id='TTutH'></center></pre></bdo></b><th id='TTutH'></th></span></q></dt></tr></i><div id='TTutH'><tfoot id='TTutH'></tfoot><dl id='TTutH'><fieldset id='TTutH'></fieldset></dl></div>
                  本文介绍了PHP cURL Content-Length 和 Content-Type 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试通过 PHP cURL 登录到一个站点,但我只收到错误请求"响应.

                  我使用了 hosts 文件并将其设置到我的服务器以检查我的浏览器发送的请求标头并将其与 cURL 发送的请求标头进行比较.

                  一切都是平等的,除了:

                  浏览器:

                  Content-Type: application/x-www-form-urlencoded内容长度:51

                  PHP 卷曲:

                  内容长度:51, 359内容类型:application/x-www-form-urlencoded;边界=----------------------------5a377b7e6ba7

                  我已经用这个命令设置了这些值,但它仍然发送错误的标头:

                  curl_setopt($this->hCurl, CURLOPT_HTTPHEADER, array('预计:','内容类型:应用程序/x-www-form-urlencoded','内容长度:51'));

                  解决方案

                  您不必自己设置内容长度.如果您使用 cURL 发送 HTTP POST,它会为您计算内容长度.

                  如果您将 CURLOPT_POSTFIELDS 值设置为数组,它会自动将请求提交为 multipart/form-data 并使用边界.如果您传递一个字符串,它将使用 application/x-www-form-urlencoded 因此请确保您将 urlencoded 字符串传递给 CURLOPT_POSTFIELDS 而不是数组,因为您需要表单-urlencoded.

                  你需要这样做:

                  $data = 'name=' .urlencode($value) .'&name2=' .urlencode($value2);curl_setopt($this->hCurl, CURLOPT_POSTFIELDS, $data);//不是$dataArray = array('name' => 'value', 'name2' => 'value2');curl_setopt($this->hCurl, CURLOPT_POSTFIELDS, $dataArray);

                  无论哪种情况,您都不需要设置内容长度,但您必须使用第一种方法在表单上获取application/x-www-form-urlencoded编码.>

                  如果这没有帮助,请发布与设置 curl 请求相关的所有代码(所有选项和您传递给它的数据),这应该有助于解决问题.

                  添加的是我想出的一个例子(我登录失败).

                   'drew', 'password' => 'testing 123');$query = http_build_query($post);curl_setopt($ch, CURLOPT_URL, $LOGIN_URL);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $query);$login = curl_exec($ch);回声 $login;

                  I'm trying to login to a site via PHP cURL and I'm only getting "Bad Request" responses.

                  I played around with hosts file and set it to my server to check which Request Headers my browser sends and compare it to the request headers sent by cURL.

                  Everything is equal, except of:

                  Browser:

                  Content-Type: application/x-www-form-urlencoded
                  Content-Length: 51
                  

                  PHP cURL:

                  Content-Length: 51, 359
                  Content-Type: application/x-www-form-urlencoded; boundary=----------------------------5a377b7e6ba7
                  

                  I already set that values with this command, but it still sends the wrong headers:

                  curl_setopt($this->hCurl, CURLOPT_HTTPHEADER, array(
                      'Expect:',
                      'Content-Type: application/x-www-form-urlencoded',
                      'Content-Length: 51' 
                  ));
                  

                  解决方案

                  You shouldn't have to set the content-length yourself. If you use cURL to send an HTTP POST, it will calculate the content length for you.

                  If you set the CURLOPT_POSTFIELDS value as an array, it will automatically submit the request as multipart/form-data and use a boundary. If you pass a string, it will use application/x-www-form-urlencoded so make sure you pass a urlencoded string to CURLOPT_POSTFIELDS and not an array since you want form-urlencoded.

                  You need to be doing this:

                  $data = 'name=' . urlencode($value) . '&name2=' . urlencode($value2);
                  curl_setopt($this->hCurl, CURLOPT_POSTFIELDS, $data);
                  
                  // NOT
                  
                  $dataArray = array('name' => 'value', 'name2' => 'value2');
                  curl_setopt($this->hCurl, CURLOPT_POSTFIELDS, $dataArray);
                  

                  In either case, you do not need to set the content length, but you have to use the first method to get application/x-www-form-urlencoded encoding on the form.

                  If that doesn't help, post all the code relevant to setting up the curl request, (all the options, and data you are passing to it) and that should help solve the problem.

                  EDIT:

                  Added is an example I came up with that works (I get failed login).

                  <?php
                  
                  $URL_HOME  = 'http://ilocalis.com/';
                  $LOGIN_URL = 'https://ilocalis.com/login.php';
                  
                  $ch = curl_init($URL_HOME);
                  curl_setopt($ch, CURLOPT_HEADER, 0);
                  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                  
                  $home = curl_exec($ch);
                  
                  //echo $home;
                  
                  $post = array('username' => 'drew', 'password' => 'testing 123');
                  $query = http_build_query($post);
                  
                  curl_setopt($ch, CURLOPT_URL, $LOGIN_URL);
                  curl_setopt($ch, CURLOPT_POST, 1);
                  curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
                  
                  $login = curl_exec($ch);
                  
                  echo $login;
                  

                  这篇关于PHP cURL Content-Length 和 Content-Type 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                  PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                  mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                  Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                  Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                  Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                      • <bdo id='BOoMg'></bdo><ul id='BOoMg'></ul>
                          <tbody id='BOoMg'></tbody>
                        <tfoot id='BOoMg'></tfoot>

                        <small id='BOoMg'></small><noframes id='BOoMg'>

                          <i id='BOoMg'><tr id='BOoMg'><dt id='BOoMg'><q id='BOoMg'><span id='BOoMg'><b id='BOoMg'><form id='BOoMg'><ins id='BOoMg'></ins><ul id='BOoMg'></ul><sub id='BOoMg'></sub></form><legend id='BOoMg'></legend><bdo id='BOoMg'><pre id='BOoMg'><center id='BOoMg'></center></pre></bdo></b><th id='BOoMg'></th></span></q></dt></tr></i><div id='BOoMg'><tfoot id='BOoMg'></tfoot><dl id='BOoMg'><fieldset id='BOoMg'></fieldset></dl></div>

                          <legend id='BOoMg'><style id='BOoMg'><dir id='BOoMg'><q id='BOoMg'></q></dir></style></legend>