How to get product by title using API REST

Solved
Tourist
4 0 1

Hello!

 

I have created the following code to get specifics products by title:

 

        $url = "https://xxxxxxx@xxxxx/admin/api/2020-04/products.json?title=ADAGIO%GOLD";
        
            
        $session = curl_init();
        curl_setopt($session, CURLOPT_URL, $url);
        curl_setopt($session, CURLOPT_CUSTOMREQUEST, "GET"); 
        curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
    
        $response = curl_exec($session); 

        curl_close($session);
    
        $json2 = json_decode($response);
    
        var_dump($json2);

 

But I get NULL.

 

What is the problem??

 

Thanks!!

0 Likes
Highlighted
Tourist
4 0 1

Nobody knows?

0 Likes
Highlighted
Explorer
78 19 19

Product title is definitely right?

What does the entire response say?

Any response headers that indicate a problem?

Can you also log the full request object?

Highlighted
Tourist
4 0 1

1-The product title is ADAGIO GOLD, and i try with ADAGIO GOLD and ADAGIO%GOLD but I get error with the both names. 

2-When i do the call with Postman i get the correct product, but when i do the call with the PHP code i get NULL [var-dump(json2) = NULL] 

Postman:

Postman.jpg

3-How can i print the headers?

4- I only get NULL with the var_dump.

 

 

0 Likes
Highlighted
Explorer
78 19 19

This is an accepted solution.

Hi Ivan

So my hunch says that you're using % when you actually mean the HTML encoded space character which is:

%20

So the name then would be: 

ADAGIO%20GOLD

Try that as the URL, e.g:

$url = "https://xxxxxxx@xxxxx/admin/api/2020-04/products.json?title=ADAGIO%20GOLD";

And see if that works?

Sorry PHP not my strong suit.

Highlighted
Tourist
4 0 1

Yeaaaaaaaaaaah, i get the product with %20.

 

Thank you so much!!!!!