Mi código es el siguiente:Enviar secuencia en la solicitud PUT con libcurl
curl = curl_easy_init();
if (curl) {
headers = curl_slist_append(headers, client_id_header);
headers = curl_slist_append(headers, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, "127.0.0.1/test.php");
curl_easy_setopt(curl, CURLOPT_PUT, 1L);
res = curl_easy_perform(curl);
res = curl_easy_send(curl, json_struct, strlen(json_struct), &io_len);
curl_slist_free_all(headers);
curl_easy_cleanup(curl);
}
Qué no funciona, el programa simplemente se cuelga siempre.
En prueba.php estos son los encabezados de la solicitud me sale:
array(6) {
["Host"]=>
string(9) "127.0.0.1"
["Accept"]=>
string(3) "*/*"
["Transfer-Encoding"]=>
string(7) "chunked"
["X-ClientId"]=>
string(36) "php_..."
["Content-Type"]=>
string(16) "application/json"
["Expect"]=>
string(12) "100-continue"
}
Pero el cuerpo está vacío, significa, no hay datos JSON se envía con la solicitud.
Lo que yo quiero hacer con libcurl es en realidad otra cosa entonces estos script de línea de comandos:
curl -X PUT -H "Content-Type: application/json" -d '... some json ...' 127.0.0.1/test.php
¡Perfecto! Y si desea evitar/suprimir la respuesta del servidor, debe configurar 'CURLOPT_WRITEFUNCTION' para recibir los datos o establecer' CURLOPT_WRITEDATA' en un manejador diferente 'FILE *'. – karlphillip
¿Por qué no utilizar 'CURLOPT_UPLOAD' en lugar de' CURLOPT_CUSTOMREQUEST', como dice [la documentación] (http://curl.haxx.se/libcurl/c/CURLOPT_PUT.html)? –
Tenía que hacer 'curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, NULL);' después de 'curl_easy_perform (curl);' para evitar que las próximas solicitudes también sean solicitudes PUT. –