-3

I have the following url, but I can't get it working with the spaces. The spaces are in $url2. How can I get the filter in the URL working? Now the filter is ignored. This means I receive all the customers instead of only the client with the specific emailaddress.

$url1 = "https://api.businesscentral.dynamics.com/v2.0/".$tenant."/Prod/ODataV4/Company('Cronus_Test')/CMS_Klant?$";
$url2 = "filter=E_Mail eq '".$row['emailadres']."'";

$curl = curl_init();
curl_setopt_array($curl, array(
            CURLOPT_URL => "".$url1."",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "GET",
            CURLOPT_GETFIELDS => "".$url2."",
            CURLOPT_HTTPHEADER => array(
                "content-type: application/x-www-form-urlencoded",
                "Authorization: Bearer ".$token.""
            ),
        ));
$response = curl_exec($curl);
curl_close($curl);
Matthijs
  • 63
  • 7
  • Why does your `$url1` contain a `$` at the end? – CBroe Aug 29 '23 at 07:04
  • I can't even find `CURLOPT_GETFIELDS` mentioned in the PHP documentation for either curl_setopt or curl_setopt_array, not sure what's up with that. I'd use `http_build_query`, and append the result as query string to the first URL directly. – CBroe Aug 29 '23 at 07:05

1 Answers1

0

You've got to url-encode your url.

In your case the space is %20

But better use urlencode() : https://www.php.net/manual/en/function.urlencode.php

CURLOPT_GETFIELDS => urlencode($url2),
Loïc
  • 11,804
  • 1
  • 31
  • 49