0

I'm using curl to post data which is retrieved from a PDO query. Each time I successfully get the expected response except that time I'm adding string with spaces. I have tried to add all the possible encoding headers for UTF8. With no result. I have searched and surfed the related questions like This Question with no luck. Below are sample of my code..

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;

} }

Community
  • 1
  • 1

1 Answers1

0

The issue is that the white spaces are not resolved in the URL in CURLOPT_URL, Simply I just replaced the spaces with %20 using str_replace to be :-

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

Thanks a lot..