2017-07-05 3 views
0

Ich habe Curl-Befehl durch Terminal arbeiten und wenn ich diesen Befehl in PHP konvertiert, gibt es mir einen Fehler. HierConverting Curl Befehl in PHP funktioniert nicht

ist Curl-Befehl vom Terminal:

[[email protected] ~]# 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}[[email protected] ~]# 

Hier ist der Curl-Befehl in PHP:

$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); 

Dies ist Antwort: 400 Bad Request

Hier ist die ausführliche Ausgabe:

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 Bad Request

+0

versuchen, die Option CURLOPT_VERBOSE zum Debuggen hinzuzufügen – murison

+0

@murison Verbose-Ausgabe zu der Frage hinzugefügt. –

Antwort

0

die Anfragen Bibliothek Verwenden Sie diese cURL Abfrage als

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); 
0

Verwenden

curl_setopt ($ ch, CURLOPT_USERAGENT ‚Mozilla/4.0 (compatible umwandeln könnte; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322) ');

oder mit urlencode ($ url);

0

diese Zeilen hinzufügen, wie Sie Ihre API nur auf POST-Anfragen reagieren, und 400-Fehler bedeutet, dass Sie API mit Non-POST-Methode fragen,

diese Zeilen hinzufügen und es sollte funktionieren ...

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");