当添加带空格的字符串时,curl_exec的json_decode返回空

时间:2015-04-30 17:07:25

标签: php json pdo

我正在使用curl发布从PDO查询中检索到的数据。每次我成功获得预期的响应,除了那时我正在添加带空格的字符串。我试图为UTF8添加所有可能的编码头。没有结果。我搜索并浏览了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;

} }

1 个答案:

答案 0 :(得分:0)

问题是在CURLOPT_URL的网址中未解析空格,我只是将spaces替换为使用%20 str_replace的{​​{1}}: -

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

非常感谢..