我有通过终端工作的curl命令,当我在PHP中转换该命令时,它给了我一个错误。
这是来自终端的Curl命令:
[root@localhost ~]# curl -XPOST -v http://localhost:5636/api/1/event/AVyWHzgsJ-3JxxYdx60x/archive
* About to connect() to localhost port 5636 (#0)
* Trying ::1...
* Connected to localhost (::1) port 5636 (#0)
> POST /api/1/event/AVyWHzgsJ-3JxxYdx60x/archive HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:5636
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< X-Evebox-Git-Revision: 8ef8639
< X-Evebox-Session-Id: IUi21/bP7TkbJ11jpYcihe1w/S41vyAbP1L1qBIJKJiL8E3440J3imOSGxKYO9j5ffqAPyv2Q3tCXqUQxhIqnw
< Date: Wed, 05 Jul 2017 06:34:31 GMT
< Content-Length: 14
<
* Connection #0 to host localhost left intact
{"status":200}[root@localhost ~]#
以下是PHP中的curl命令:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localhost:5636/api/1/event/AVyWHzgsJ-3JxxYdx60x/archive");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
这是回复:400 Bad Request
以下是详细输出:
Verbose information:
* About to connect() to localhost port 5636 (#6) * Trying ::1... * Connected to localhost (::1) port 5636 (#6) > POST /api/1/event/AVyWHzgsJ-3JxxYdx60x/archive HTTP/1.1 Host: localhost:5636 Accept: */* Content-Length: -1 Content-Type: application/x-www-form-urlencoded Expect: 100-continue < HTTP/1.1 400 Bad Request < Content-Type: text/plain; charset=utf-8 < Connection: close < * Closing connection 6
400错误请求
答案 0 :(得分:0)
使用Requests库,您可以将该cURL查询转换为
include('vendor/rmccue/requests/library/Requests.php');
Requests::register_autoloader();
$headers = array();
$response = Requests::post('http://localhost:5636/api/1/event/AVyWHzgsJ-3JxxYdx60x/archive', $headers);
答案 1 :(得分:0)
使用
curl_setopt($ ch,CURLOPT_USERAGENT,&#39; Mozilla / 4.0(兼容; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)&#39;); < / p>
或使用urlencode($ url);
答案 2 :(得分:0)
添加此行,因为您的API只会响应POST请求,而400错误意味着您要求API使用非POST方法,
添加此行,它应该有用......
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");