1

I tested an API using postman. Using, postman, I managed to generate a token.

From postman, I managed to get all the data of a user using the following with the following details via a GET request:

https://example.example.co.za/api/consumers/get?id=567675675&[email protected] 

Under Authorization type, I selected "Bearer token" and pasted the Token in its respective field.

When I click send, I get a success response with the user data.

In PHP, how can I do the same api call (using the id,email and token) inside a php function?

I tried this:

curl_setopt_array($curl, array( CURLOPT_URL => "example.example.co.za/api/consumers/[email protected]&id=567675675", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_POSTFIELDS => "", CURLOPT_HTTPHEADER => array( "Accept: */*", "Authorization: Bearer jhgukfytjfytdytfjgjyfytfjkugfyfdhtklhkugjf", "Cache-Control: no-cache", "Connection: keep-alive", "Content-Type: application/x-www-form-urlencoded", ), )); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); return $response; 

But when I view the page, instead of the user data, I see this:

enter image description here

How do I make the API call inside a function to get the same result that I have on postman in PHP?

4
  • 4
    A valid url needs a scheme like https://. Commented Aug 26, 2019 at 14:27
  • It's a redirect you are seeing. What's the actual URL look like? Commented Aug 26, 2019 at 14:28
  • 1
    @MarkusZeller curl is flexible, it tries to parse the URL similarly to the way browsers do in the address bar. So it will figure out that example.example.co.za is the domain and the scheme defaults to http:. The redirect is presumably the server try to switch it to https: Commented Aug 26, 2019 at 14:32
  • 1
    I combined @MarkusZeller comment with Bob's answer and now I am able to see the data, Commented Aug 26, 2019 at 14:41

2 Answers 2

3

You need to follow the location

CURLOPT_FOLLOWLOCATION => true 

https://www.php.net/manual/de/function.curl-setopt.php

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

Comments

0

Please find a proper CURL call here.

 $url = "https://example.example.co.za/api/consumers/get?id=567675675&[email protected]"; $curl = curl_init(); // OPTIONS: curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HTTPHEADER, array( 'APIKEY: 111111111111111111111', 'Content-Type: application/json', )); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); // EXECUTE: $result = curl_exec($curl); if(!$result){die("Connection Failure");} curl_close($curl); 

1 Comment

what if I want the api key to be stored in a variable first? How would the full snippet look like?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.