1

I'm trying to call a URL from PHP via cURL, but no response from server.

Following is my URL:

$url = 'https://id:[email protected]/oauth/token \ -X POST \ -d "grant_type=client_credentials&scope=manage_project:test"';

Curl code:

$auth = curl_init($url);
curl_setopt($auth, CURLOPT_POST, true);
curl_setopt($auth, CURLOPT_HEADER, false);
curl_setopt($auth, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($auth);
curl_close($auth);
print_r($response);

When I execute the same URL in terminal get response from server. What's wrong in cURL code?

5
  • 1
    The URL does not look right = seems to be the command line options to curl Commented Feb 24, 2017 at 4:26
  • CURL is installed in your web server, right? Commented Feb 24, 2017 at 4:27
  • Yes, curl is installed Commented Feb 24, 2017 at 4:27
  • I don't see the data parameter in your code Commented Feb 24, 2017 at 4:31
  • @AkkapongKajornwongwattana what data parameter?? Commented Feb 24, 2017 at 4:33

3 Answers 3

3

Check the Curl Snippet

 $auth = curl_init("https://id:[email protected]/oauth/token");
    curl_setopt($auth, CURLOPT_POST, true);
    curl_setopt($auth, CURLOPT_HEADER, false);
    curl_setopt($auth, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($auth,CURLOPT_POST, 2);
    curl_setopt($auth,CURLOPT_POSTFIELDS, "grant_type=client_credentials&scope=manage_project:test");
    $response = curl_exec($auth);
    curl_close($auth);
    print_r($response);

provide the correct credentials in order for the authentication to be successful

Sign up to request clarification or add additional context in comments.

Comments

1

Using the documentation - this should work. Note the correct URL and passing the post parameters correctly

$auth = curl_init("https://auth.sphere.io/oauth/token");
curl_setopt($auth, CURLOPT_POST, true);
curl_setopt($auth, CURLOPT_HEADER, false);
curl_setopt($auth, CURLOPT_RETURNTRANSFER, true);
curl_setopt($auth,CURLOPT_POST, 2);
curl_setopt($auth,CURLOPT_USERPWD, "id:secret");
curl_setopt($auth,CURLOPT_POSTFIELDS, "grant_type=client_credentials&scope=manage_project:test");
$response = curl_exec($auth);
curl_close($auth);
print_r($response);

5 Comments

Tried. Getting Missing required parameter: grant_type error.
Is that a response from the server?
still same . {"statusCode":400,"message":"Missing required parameter: grant_type.","errors":[{"code":"invalid_request","message":"Missing required parameter: grant_type."}],"error":"invalid_request","error_description":"Missing required parameter: grant_type."}
Just noticed that I made an error in the cut'n'paste
1

@Ed Heal - modifying your code following should work

$auth = curl_init("https://auth.sphere.io/oauth/token");
curl_setopt($auth, CURLOPT_POST, true);
curl_setopt($auth, CURLOPT_HEADER, false);
curl_setopt($auth, CURLOPT_RETURNTRANSFER, true);
curl_setopt($auth,CURLOPT_POST, 2);
curl_setopt($auth,CURLOPT_USERPWD, "id:secret");
curl_setopt($auth,CURLOPT_POSTFIELDS, "grant_type=client_credentials&scope=manage_project:test");
$response = curl_exec($auth);
curl_close($auth);
print_r($response);

$ch is problem as curl is initialize with $auth

Comments

Your Answer

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.