0

I am trying to create a web app that gets data from a Magento 2 website thanks to the API, following this tutorial but it returns bool(false) when I do var_dump($token)

I have read other threads on StackOverflow saying it might be linked to an error with SSL, but I have no error when I use curl_error($ch) so I don't think that's the issue.

My code :

 $userData = array("username" => $username, "password" => $password); 
 $ch = curl_init("http://mywebsite/index.php/rest/V1/integration/admin/token"); 
 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData)); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Length: " . strlen(json_encode($userData))));
 $token = curl_exec($ch);

Thank you for reading.

EDIT: The account I use has a role with all permission so that shouldn't be the issue either.

EDIT 2: When I change the Magento URL and account to the ones from this demo it returns me a token and the information I want, but not with the Magento I am working on.

asked Oct 12, 2017 at 15:39
0

1 Answer 1

0

Try to do this code which is quite similar:

//API URL
$url = 'http://mywebsite/rest/V1/integration/admin/token';
//create a new cURL resource
$ch = curl_init($url);
//setup request to send json via POST
$data = array(
 'username' => '.....',
 'password' => '.....'
);
$payload = json_encode(array("user" => $data));
//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
//set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
//return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute the POST request
$result = curl_exec($ch);
//close cURL resource
curl_close($ch);

You can try also to follow the redirect :

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

You can also add these lines in order to debug your CURL request in a custom file.

curl_setopt($handle, CURLOPT_VERBOSE, true);
$verbose = fopen('/tmp/curl.log', 'w+');
curl_setopt($handle, CURLOPT_STDERR, $verbose);

Moreover, first try to test you API thanks to Postman or other software to know if the problem is on the curl / php side.

What is your webserver ? Apache / Nginx ? Do you use the default Magento 2 configuration ?

answered Oct 13, 2017 at 7:44
8
  • It's Nginx I think and I don't know much about the Magento2 configuration as I just started working on this project today. I managed to find that it isn't an issue with my code, as when I try to use it with another Magento 2 it works (I found this demo which does return the token) Commented Oct 13, 2017 at 9:43
  • I started working on this project today but it was started by someone else, that's why I said that ^^ Commented Oct 13, 2017 at 10:01
  • Did you try to retrieve the token with Postman ? Commented Oct 13, 2017 at 13:38
  • I do get the token for the website with Postman, but not with my code. Even though the exact same code with the demo I linked earlier works perfectly. (Also, you just taught me what Postman was and it is very nice indeed, thanks :) ) Commented Oct 13, 2017 at 14:32
  • Can you check to add the verbose and check the log ? Commented Oct 16, 2017 at 8:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.