online kali invalid convert php curl

kali - Convertir línea de comando cURL en PHP cURL



linux convert base64 to file (7)

Nunca he hecho ningún curl antes, así que necesito ayuda. ¡He intentado resolver esto a partir de ejemplos, pero no puedo entenderlo!

Tengo un comando curl que puedo ejecutar con éxito desde una línea de comandos de linux (ubuntu) que coloca un archivo en una wiki a través de una API.

Necesitaría incorporar este comando curl en un script PHP que estoy construyendo.

¿Cómo puedo traducir este comando curl para que funcione en un script PHP?

curl -b cookie.txt -X PUT / --data-binary "@test.png" / -H "Content-Type: image/png" / "http://hostname/@api/deki/pages/=TestPage/files/=test.png" / -0

cookie.txt contiene la autenticación, pero no tengo problemas para poner esto en texto claro en el script, ya que solo lo ejecutaré yo.

@ test.png debe ser una variable como $ filename

http://hostname/@api/deki/pages/=TestPage/files/= debe ser una variable como $ pageurl

Gracias por cualquier ayuda.


Cualquiera que sea el CURL que tenga en la línea de comando, puede convertirlo a PHP con esta herramienta:

https://incarnate.github.io/curl-to-php/

¡Me ayudó después de largas horas de búsqueda de una solución! Espero que te ayude también! Tu solución es esta:

// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://hostname/@api/deki/pages/=TestPage/files/=test.png"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $post = array( "file" => "@" .realpath("test.png") ); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); $headers = array(); $headers[] = "Content-Type: image/png"; curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($ch); if (curl_errno($ch)) { echo ''Error:'' . curl_error($ch); } curl_close ($ch);


Mejor esto. En una linea

$cmd=''curl -b cookie.txt -X PUT --data-binary "@test.png" -H "Content-Type: image/png" "http://hostname/@api/deki/pages/=TestPage/files/=test.png" -0''; exec($cmd,$result);



Prueba esto:

$cmd=''curl -b cookie.txt -X PUT / --data-binary "@test.png" / -H "Content-Type: image/png" / "http://hostname/@api/deki/pages/=TestPage/files/=test.png" / -0''; exec($cmd,$result);


Usando la respuesta de MYYN como punto de partida, y esta página como referencia sobre cómo enviar datos POST utilizando PHP cURL, aquí está mi sugerencia (estoy trabajando en algo muy similar en este momento):

<?php $pageurl = "http://hostname/@api/deki/pages/=TestPage/files/="; $filename = "test.png"; $theurl = $pageurl.$filename; $ch = curl_init($theurl); curl_setopt($ch, CURLOPT_COOKIE, ...); // -b curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ''PUT''); // -X curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary curl_setopt($ch, CURLOPT_HTTPHEADER, [''Content-Type: image/png'']); // -H curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // -0 $post = array("$filename"=>"@$filename"); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $response = curl_exec($ch); ?>

Probablemente pueda optimizar los muchos curl_setopts con el uso de una llamada curl_setopt_array () si lo desea.


la opción --libcurl fue agregada para este propósito, a pesar de que hace un programa C, creo que debería ser bastante fácil de traducir a PHP


un punto de partida:

<?php $pageurl = "http://hostname/@api/deki/pages/=TestPage/files/="; $filename = "test.png"; $theurl = $pageurl . $filename; $ch = curl_init($theurl); curl_setopt($ch, CURLOPT_COOKIE, ...); // -b curl_setopt($ch, CURLOPT_CUSTOMREQUEST, ''PUT''); // -X curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary curl_setopt($ch, CURLOPT_HTTPHEADER, [''Content-Type: image/png'']); // -H curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); // -0 ... ?>

Ver también: http://www.php.net/manual/en/function.curl-setopt.php