kevinkt
kevinkt

Reputation: 745

How do I translate this curl request to PHP?

I'm trying to integrate a product and the example curl request is as follows.

curl https://a.klaviyo.com/api/v1/lists -G \
  -d api_key=pk_e29b4ec921f6aed9a70eb1e6993bb5caed

What I don't understand is what does -G and -d signify and how do I translate this request into PHP code?

Upvotes: 1

Views: 472

Answers (3)

AZinkey
AZinkey

Reputation: 5319

-G, --get

When used, this option will make

all data specified with -d, --data, --data-binary or --data-urlencode to be used in an HTTP GET request instead of the POST

request that otherwise would be used. The data will be appended to the URL with a '?' separator.

If used in combination with -I, --head, the POST data will instead be appended to the URL with a HEAD request.

If this option is used several times, only the first one is used. This is because undoing a GET doesn't make sense, but you should then instead enforce the alternative method you prefer.

$ch = curl_init();

$data = array('api_key'=>"pk_e29b4ec921f6aed9a70eb1e6993bb5caed");
curl_setopt($ch, CURLOPT_URL, "https://a.klaviyo.com/api/v1/lists");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_HTTPGET, 1);

$output = curl_exec($ch);
$info = curl_getinfo($ch);

Upvotes: 0

Er Nilay Parekh
Er Nilay Parekh

Reputation: 587

Just try this code

 $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://a.klaviyo.com/api/v1/lists?api_key=pk_e29b4ec921f6aed9a70eb1e6993bb5caed');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $response = curl_exec($ch);     
    curl_close($ch);
    // Decode the response into a PHP associative array
    $response = json_decode($response, true);   
    echo "<pre>";print_r($response);

Upvotes: 0

madalinivascu
madalinivascu

Reputation: 32354

-G stand for a get request and -d is the data passed to the get

in php you do

file_get_contents('https://a.klaviyo.com/api/v1/lists?api_key=pk_e29b4ec921f6aed9a70eb1e6993bb5caed');

Upvotes: 1

Related Questions