PHP curl exec

Gast #5040637
Lesenswert?

Hallo zusammen,
Ich versuche vergeblich folgenden curl Befehl
1
curl \
2
  -X PATCH \
3
  --header "Content-Type:application/json" \
4
  --data '{"type": "value", "valueOrExpression": 1337}' \
5
  --user "user:password" \
6
  http://test/api/variables/the-answer


In PHP darzustellen hier mein Versuch
1
$ch = curl_init();
2

3
$data = array('value':'1337');
4

5
curl_setopt($ch, CURLOPT_URL, 'http://Test/api/variables/the-answer');
6
curl_setopt($ch, CURLOPT_POST, 1);
7
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
8

9
curl_exec($ch);

Aber es klappt nicht?!
Gast #5040664
Lesenswert?

Also so?

1
$ch = curl_init();
2

3
$data = array('value':'1337');
4

5
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
6
    'Content-Type: application/json',                                                                                
7
    'Content-Length: ' . strlen($data_string))  
8

9

10
curl_setopt($ch, CURLOPT_URL, 'http://Test/api/variables/the-answer');
11
curl_setopt($ch, CURLOPT_POST, 1);
12
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
13

14
curl_exec($ch);
Gast #5040854
Lesenswert?

Mein Ansatz
1
 $data = array("value" => "133");
2
$data_string = json_encode($data);
3
$ch = curl_init();
4
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
5

6

7
curl_setopt($ch, CURLOPT_URL, 'http://test/api/variables/variable_string_today');
8
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
9
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
10
curl_exec($ch);
11

12
if(curl_error($ch))
13
{
14
    echo 'error:' . curl_error($ch);
15
}


und der Fehler
1
error:Empty reply from server
Gast #5041788
Lesenswert?

Vergleich mal:

Skriptanfänger schrieb:
> --data '{"type": "value", "valueOrExpression": 1337}' \

mit

Skriptanfaenger schrieb:
> $data = array("value" => "133");
> $data_string = json_encode($data);

Denn das ist nicht identisch.


Also: Debug-Ausgabe für "$data_string" einbauen, z.B. mit error_log.

Oder, wie vorgeschlagen, mit Wireshark/ngrep/tcpdump/whatever 
nachschauen, was tatsächlich über's Netz geht.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren