1

I'm trying to make a search request which returns all the customers, but the php script below returns :

[message] => %fieldName is a required field.
[parameters] => Array
    (
        [fieldName] => searchCriteria
    )

My PHP script :

<?php

$search = '
{
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "name",
                        "value": "%",
                        "condition_type": "like"
                    }
                ]
            }
        ],
        "current_page": 1,
        "page_size": 1
    }
}
';

$token = "xxxxxxxxxxxxxxxxx";

$api = "rest/V1/customers/search";

$ch = curl_init("http://my.website.com/index.php/".$api);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $search);
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer ".$token));

$result = curl_exec($ch);

$result = json_decode($result, 1);

print_r($result);

?>

What's wrong with this request ?

This other way to write this request works fine:

curl -X GET "http://my.website.com/index.php/rest/V1/customers/search? \
searchCriteria[filterGroups][0][filters][0][field]=email& \
searchCriteria[filterGroups][0][filters][0][value]=%& \
searchCriteria[filterGroups][0][filters][0][condition_type]=like" \
-H "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxx" \
-H "Content-type: application/json" -g
3

I finally fixed my issue this way :

<?php    

$token = "xxxxxxxxxxxxxxxxxxxx";
$api = "rest/V1/customers/search";

$json = '
{
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "email",
                        "value": "%",
                        "condition_type": "like"
                    }
                ]
            }
        ]
    }
}
';
$j = json_decode($json);
$get_params = http_build_query($j);

$ch = curl_init("http://my.website.com/index.php/".$api."?".$get_params);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  "Content-Type: application/json", 
  "Authorization: Bearer ".$token,
));

$result = curl_exec($ch);
$result = json_decode($result, 1);
print_r($result);

?>
0
0

You have to mention at-least one searchCriteria because search Criteria is required field (for example:- /rest/V1/customers/search?searchCriteria[sortOrders][][direction]=ASC).

6
  • I have add "sort_orders": [ { "field": "name", "direction": 1 } ] right after the page_size, but I still get the same error message. Also, the curl command works without sort_orders.
    – DevonDahon
    May 4 '17 at 4:38
  • did you checked my code(use only searchCriteria[sortOrders][][direction]=ASC and check) May 4 '17 at 4:42
  • it is working for me....i got data also.. May 4 '17 at 4:44
  • no need to mention all search criteria ..but we have to write at least one search criteria. May 4 '17 at 4:46
  • I'm sorry I don't understand your answers. My problem is about correcting the json in the question.
    – DevonDahon
    May 4 '17 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.