如何解决 curl php 中的 HTTP/1.1 400 错误请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32944441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:17:40  来源:igfitidea点击:

How to resolve HTTP/1.1 400 Bad Request in curl php

phpcurl

提问by Sanjay Nakate

I have to hit one .aspxpage url from php code i am trying to hiting using curl but i am getting bellow error and there is no white space in url.

我必须.aspx从 php 代码中点击一页 url 我正在尝试使用 curl 进行点击,但出现以下错误并且 url 中没有空格。

HTTP/1.1 400 Bad Request Content-Type: text/html; charset=us-ascii Server: Microsoft-HTTPAPI/2.0 Date: Mon, 05 Oct 2015 08:31:13 GMT Connection: close Content-Length: 311 

Bellow is the code of curl which i am trying to hit.so any body will tell why iam getting this error.

Bellow 是我试图点击的 curl 代码。所以任何机构都会告诉我为什么我会收到这个错误。

$api_url = 'http://www.test/xyz/OnlineOrder.aspx?';
     $url= $api_url . 'InvoiceNo=' . $invoice;
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_HEADER, 1);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     echo $data = curl_exec($ch);
     echo $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

回答by Sanjay Nakate

I got the Http response 200 okI have observed carefully the url and i found one parameter with white space that was cause the Bad request.Any way if you got some thing like problem remove the white space from url and there parameter.

我得到了Http response 200 ok我仔细观察了 url,我发现一个带有空格的参数是导致错误请求的原因。无论如何,如果您遇到类似问题,请从 url 和那里参数中删除空格。

using urlencode($url);

使用 urlencode($url);

回答by softech

If it help anyone, my issue is I have to use POSTmethod in curl but no POSTparameters has been require, as API grab value from URL query string only. So, passing empty array in POSTFIELDSfixed my problem. My guess is it depends on Apache/PHP setting on server, whether it allows POSTrequest without post parameters or not.

如果它对任何人有帮助,我的问题是我必须POST在 curl 中使用方法,但不需要POST参数,因为 API 仅从 URL 查询字符串中获取值。所以,传递空数组POSTFIELDS解决了我的问题。我的猜测是这取决于服务器上的 Apache/PHP 设置,是否允许POST不带 post 参数的请求。