Home > OS >  send json curl request from php with line breaks
send json curl request from php with line breaks

Time:11-15

I am trying to convert CLI curl to php. I have tried this manually, I have tried using scripts, I have tried searching for solutions, I can't find anything useful relating to sending json curl requires containing line breaks using php.

curl -X POST "https://url.com" -H "accept: application/json" -H "X-API-Key: API_KEY" -H "Content-Type: application/json" -d '{"key1": "value1", "key2": "value2", "description_key": "{\n\"inner_text_key1\": \"inner_text_value\"\n}" }'

So I have a curl that works from the CLI without problem that is similar to above. Note that the description contains a STRING that is formatted as if it were json. This is correct.

However whenever I try to add the line breaks in on php, the whole things breaks. The api I am working with ends up returning "unescaped characters" on the \n

How do I properly format in such a way that the line breaks are sent with the request and not interpreted as unescaped characters?

If I remove the line breaks from the request it will send perfectly. Equally doing so from the CLI works perfect, just can't get it to work with php AND the line breaks.

The problem is not really the php itself as stated, it works without the \n none the less the php is below.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://url.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"key1\": \"value1\", \"key2\": \"value2\", \"description_key\": \"{\n\"inner_text_key1\": \"inner_text_value\"\n}\"}");
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'X-Api-Key: no';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
  echo 'Error:' . curl_error($ch);
}
curl_close($ch);

echo '<pre>';
var_dump($result);
echo '</pre>';

CodePudding user response:

I tried everything that had been suggested, and none of it worked. Not even encoding and decoding. In the end... the fix was simple.

Originally I had:

curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"key1\": \"value1\", \"key2\": \"value2\", \"description_key\": \"{\n\"inner_text_key1\": \"inner_text_value\"\n}\"}");

I merely changed that to

curl_setopt($ch, CURLOPT_POSTFIELDS, '{"key1": "value1", "key2": "value2", "description_key": \"{\n\"inner_text_key1\": \"inner_text_value\"\n}"}');

So I removed all the escapes on the " except the part of the inner json "description": area and used a single quote around the entire thing. Suddenly the line breaks work and all is submitted properly.

CodePudding user response:

Seems like json encoded in json.

echo var_export(
    json_decode(
        '{"key1": "value1", "key2": "value2", "description_key": "{\n\"inner_text_key1\": \"inner_text_value\"\n}" }'
        , true
    ), true
);
// array(
//     'key1'            => 'value1',
//     'key2'            => 'value2',
//     'description_key' => '{
// "inner_text_key1": "inner_text_value"
// }',
// )

Json decoding description_key on the json decoded string works.
So you got a problem on the source of the string.

If you cannot change that then

  • json decode the string
  • json decode the description_key offset
  • json encode the array again (once)
  • use it on curl

EDIT:

Understood: json in array is expected. Line break is the problem.

Then you could just decode, encode:

    $jsonString = '{"key1": "value1", "key2": "value2", "description_key": "{\n\"inner_text_key1\": \"inner_text_value\"\n}" }';
    $array = json_decode($jsonString, true);
    // array (
    //     'key1' => 'value1',
    //     'key2' => 'value2',
    //     'description_key' => '{"inner_text_key1":"inner_text_value"}',
    // )
    if ($array['description_key']
        and is_string($array['description_key'])
    ) {
        $array['description_key'] = json_decode($array['description_key'], true);
        // array (
        //     'key1' => 'value1',
        //     'key2' => 'value2',
        //     'description_key' => array (
        //         'inner_text_key1' => 'inner_text_value',
        //     ),
        // )
        // If you NEED it to be encoded in encoded json then:
        $array['description_key'] = json_encode($array['description_key']);
        // array (
        //     'key1' => 'value1',
        //     'key2' => 'value2',
        //     'description_key' => '{"inner_text_key1":"inner_text_value"}',
        // )
    }
    $jsonString = json_encode($array);
    // '{"key1":"value1","key2":"value2","description_key":"{\\"inner_text_key1\\":\\"inner_text_value\\"}"}'
  • Related