php - 如何在 curl php 中解决 HTTP/1.1 400 Bad Request

标签 php curl

我必须从 php 代码中点击一个 .aspx 页面 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 是我试图命中的 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);

最佳答案

我得到了 Http response 200 ok 我仔细观察了 url,我发现一个带有空格的参数导致了错误的请求。如果你遇到类似问题的任何方法,请删除白色来自 url 的空格和那里的参数。

使用 urlencode($url);

关于php - 如何在 curl php 中解决 HTTP/1.1 400 Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32944441/

相关文章:

json - 创建索引模板时,JSON解析异常

javascript - 使用 $.Ajax 调用 UBER api

php - 基于另一个关联数组的值构建关联数组

php - Laravel 作业队列不使用 Redis 驱动程序处理

php - Magento 1.14 中的 Paypal GetExpressCheckoutDetails 响应中缺少 SHIPTONAME 数组元素

php - 我如何检查我的 WordPress 代码是否正确?

Python:PyCurl 设置 URL 异常

php - 如何使用socket或curl连接到mysql

curl - Istio 特使正在丢弃带有 Host header 的请求

php - 使用 PHP 从 JSON 字符串转换 Unicode