0

Hi I have this code I am trying to send a string and an array to an api but I do not receive any error and I can not make it work.

Can someone help me and tell me what I'm doing wrong?

this is my code:

<?php
$url = "https://example.url/api/links/create?api_token=lpwuF8R7A6y41fg64fdgvBydQX6T7I64waaPf";

$curl = curl_init();

$params = json_encode([
    'long_url' => $lnk,
    'rules' => '{key: "us", value: "https://destination.us", type: "geo"}',
]);

curl_setopt_array($curl, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => $url,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $params,
    CURLOPT_HTTPHEADER => [
        'Content-Type: application/json',
        'Content-Length: ' . strlen($params)
    ]
]);
$response = json_decode(curl_exec($curl));
curl_close($curl);


// $response can now be used, see above for how it's formate
$data2 = $response;

 ?>
3
  • What does curl_getinfo($curl) return? Commented Oct 8, 2020 at 21:20
  • Don't make your own JSON. Use the proper functions to do so. Commented Oct 8, 2020 at 22:05
  • @miken32 he does set the Content-Type. But the JSON itself is wrong, it lacks the quotes in the property names. Commented Oct 8, 2020 at 22:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.