Saltar al contenido

ejemplo de código php a curl

Ya no busques más por todo internet porque has llegado al espacio justo, contamos con la solución que buscas y sin complicaciones.

Ejemplo 1: publicación de php curl

// set post fields
$post=['username'=>'user1',
    'password'=>'passuser1',
    'gender'=>1,
];$ch= curl_init('http://www.example.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

// execute!$response= curl_exec($ch);

// close the connection, release resources used
curl_close($ch);

// do anything you want with your response
var_dump($response);

Ejemplo 2: ejemplo de php curl

function getUrl($url)$ch= curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);$response= curl_exec($ch);
    curl_close($ch);return$response;

Ejemplo 3: curl en php

// Get cURL resource
$curl= curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER =>1,
    CURLOPT_URL =>'http://testcURL.com',
    CURLOPT_USERAGENT =>'Codular Sample cURL Request',
    CURLOPT_POST =>1,
    CURLOPT_POSTFIELDS =>[
        item1 =>'value',
        item2 =>'value2']]);
// Send the request & save response to $resp$resp= curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);

Ejemplo 4: curl_init () en php

<?php
// create a new cURL resource
$ch= curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);
?>

Ejemplo 5: ejemplo de curl php

function makeAPICall($url)$handle= curl_init();

         
        // Set the url
        curl_setopt($handle, CURLOPT_URL, $url);
        // Set the result output to be a string.
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);$output= curl_exec($handle);
         
        curl_close($handle);echo$output;return$output;

Ejemplo 6: php usa curl

It is important to notice that when using curl to post form data and you use an array for CURLOPT_POSTFIELDS option, the post will be in multipart format<?php
$params=['name'=>'John', 'surname'=>'Doe', 'age'=>36)$defaults= array(
CURLOPT_URL =>'http://myremoteservice/', 
CURLOPT_POST => true,
CURLOPT_POSTFIELDS =>$params,
);$ch= curl_init();
curl_setopt_array($ch, ($options + $defaults));
?>
This produce the following post header:

--------------------------fd1c4191862e3566
Content-Disposition: form-data;name="name"

Jhon
--------------------------fd1c4191862e3566
Content-Disposition: form-data;name="surnname"

Doe
--------------------------fd1c4191862e3566
Content-Disposition: form-data;name="age"36
--------------------------fd1c4191862e3566--

Setting CURLOPT_POSTFIELDS as follow produce a standard post header

CURLOPT_POSTFIELDS => http_build_query($params),

Which is:
name=John&surname=Doe&age=36

This caused me 2 days of debug while interacting with a java servicewhich was sensible to this difference, while the equivalent one in php got both format without problem.

Si guardas algún reparo y forma de arreglar nuestro post puedes añadir una disquisición y con deseo lo analizaremos.

¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)



Utiliza Nuestro Buscador

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *