json_decode for curl_exec returns 添加带空格的字符串时为空

json_decode for curl_exec returns empty when adding string with spaces

我正在对从 PDO 查询中检索到的 post 数据使用 curl。每次我成功获得预期的响应时,除了那次我添加带空格的字符串。我试图为 UTF8 添加所有可能的编码 headers。没有结果。我搜索并浏览了 This Question 等相关问题,但没有成功。下面是我的代码示例..

public function collectorFetcher(){
$data = $this->pdo->prepare(" select * bla bla ");
$data->execute();
$result = $data->fetchAll();
foreach($result as $key => $value){
   //echo $result[$key]['customer_name'];die;
   curl_setopt($this->ch, CURLOPT_URL, "http://API_URL/'customerRecords':[{'arabicData':'".$result[$key]['arabicData']."'}]}");
   curl_setopt($this->ch, CURLOPT_HEADER, 0);
   curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml;charset=utf-8"));
   $response = curl_exec($this->ch);
   $data = json_decode($response);
   var_dump($data);die;
   if(isset($data->beanResponse->inserted)){
     $this->log("Insert Status: ".$data->beanResponse->inserted);
   }else if(isset($data->beanResponse->message)){
    $this->log("Collector Fetcher message: ".$data->beanResponse->message);
  }else{
    $this->log("Collector Fetcher General Error: ".var_dump($data));
  }die;

} }

问题是 CURLOPT_URL 中的 URL 中的空格没有解决,我只是用 str_replacespaces 替换为 %20 ] 为:-

curl_setopt($this->ch, CURLOPT_URL, str_replace(' ', '%20',"http://API_URL/'customerRecords':[{'arabicData':'".$result[$key]['arabicData']."'}]}"));

非常感谢..