PHP 当我传递变量时,Curl 不起作用

PHP Curl does not work when I pass a variable

<?php
class Updater {

  function addTextOnImage($id,$text_on_image){
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://example.com/example/items/message/".$id,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "PATCH",
       CURLOPT_POSTFIELDS =>"{\"text_on_image\": \"".$text_on_image."\"}",
      CURLOPT_HTTPHEADER => array(
        "Authorization: Bearer token",
        "Content-Type: application/json",
        "Cookie: PHPSESSID=ssid"
      ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
  }

}

我执行addTextOnImage函数。

如果我这样执行:

$value= "A test string";
$updater->addTextOnImage($id,$value);

有效。 但如果我这样做:

$value= strval($result);
$updater->addTextOnImage($id,$value);

我收到空的或错误的Paylodad 异常。 $value 的 var_dump pf(在第二个版本中不起作用)看起来像:

sring(68)“如果动物能做到”

两个都是字符串,为什么第二个就不行了?

我找到了解决方案:

$data = array(
      'text_on_image' => $text_on_image
    );
$payload = json_encode($data);