0

I am new to the Visa Developer Platform (VDP) APIs and I am running into issues trying to read the output of the response as a json using php. I followed the tutorial here. I am using the Offers Data API.

My goal is to be able to generate a list of deals to be displayed in the frontend. I am trying to do this by reading the json output and parsing the info for the deals.  

This is what the original tutorial had and it works fine:

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_PORT, 443);
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
curl_setopt($curl, CURLOPT_SSLCERT, $cert);
curl_setopt($curl, CURLOPT_SSLKEY, $key);

curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($curl);  //This output contains the json output as well

$response_info = curl_getinfo($curl); 

To get the information, I am running: $response_data = file_get_contents($response); which does not appear to work. Since the output it not just the json, but with other info, I am not able to run: $arr = json_decode($response,true); to parse the json. This is what the json looks like:

{"ReturnedResults":4,"StartIndex":1,"Offers":[{"indexNumber":1,"offerContentId":105515,"offerId":

and so on. The json starts with {"indexNumber":1 and everything before it needs to be discarded. Please let me know what I can do to fix this, or if there is a better way to accomplish the goal.  Thank you!

  • `tutorial here`...where? There's no link provided. `Since the output it not just the json`...so what exactly does it look like? Give an example. `what I can do to fix this`...we have no idea, because you did not link to any documentation, did not show us what cURL request you made, did not show us what response you got, or what response you expected. We have literally no clue what specfically is happening. Vague descriptions and partial details are not useful information. Please provide a [mre] of the issue so we can give practical assistance. See also [ask] for more general guidance. Thanks. – ADyson Nov 16 '21 at 22:59
  • when the responce is not only json then you have to parse it somehow, either by provided solution or your own. note: function `file_get_contents` expect filename parameter not the responce content, could be used as partial alternative to the curl but thats another topic – Kazz Nov 16 '21 at 23:59
  • Sorry, I forgot to add the links before posting. I updated the post. @Kazz is there an alternative I could use? Maybe I can store the information in a file then read the file? – MiPlayer123 Nov 17 '21 at 18:26
  • Please re-read my comment and provide _all_ the information requested, not just some of it. – ADyson Nov 17 '21 at 18:42
  • Also if you're following that tutorial, why didn't you implement all the code they've shown for dealing with the response? That might help you see what's going on more easily. P.s. It's true that file_get_contents makes zero sense here, because $response is not a file. – ADyson Nov 17 '21 at 18:44
  • I updated the post. Hopefully, it makes more sense now. The original tutorial does not have the part to parse the JSON as it only has how to conned to the API. – MiPlayer123 Nov 18 '21 at 02:29
  • `Since the output it not just the json, but with other info`...so again, what other info is there? We still don't know what else you're seeing. Knowing what this is would enable us to try and understand why it's there, and also how you can get rid of it and/or ignore it in order to be able to just parse the JSON – ADyson Nov 18 '21 at 10:03
  • 1
    As a guess, it _could_ be because you've got `curl_setopt($curl, CURLOPT_VERBOSE, 1);` set - this will make cURL output all sorts of debugging information which you should not need in production. You should either remove that line, or use a second option to redirect the output to a separate stream (as per the instructions at https://stackoverflow.com/a/14436877/5947043) – ADyson Nov 18 '21 at 10:04

2 Answers2

1

Goal

The json starts with {"indexNumber":1 and everything before it needs to be discarded. Please let me know what I can do to fix this, or if there is a better way to accomplish the goal.

Code

The response variable contains a valid json object. Since you only need Offers you can use this code to obtain the Offers:

$response = '{"ReturnedResults":4,"StartIndex":1,"Offers":[{"indexNumber":1,"offerContentId":105515,"offerId":""}]}';

$json = json_decode($response, true);
var_dump($json['Offers']);

Output

array(1) {
  [0]=>
  array(3) {
    ["indexNumber"]=>
    int(1)
    ["offerContentId"]=>
    int(105515)
    ["offerId"]=>
    string(0) ""
  }
}
Douma
  • 2,682
  • 14
  • 23
  • This works! `$json = json_decode($response, true);` was the key. From there I used a foreach loop to get the data i need: foreach($offers as $x => &$offer) { print($offer["offerId"]."
    "); print($offer["offerTitle"]."
    "."
    "); }`
    – MiPlayer123 Nov 20 '21 at 16:25
0

Init cURL with CURLOPT_RETURNTRANSFER => true - then you will have your content in $response variable. Without that option, result of curl_exec is always boolean.

aso
  • 1,331
  • 4
  • 14
  • 29
  • I am using `curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);` and then `$response = curl_exec($curl);`. this part works, but then it is not in a json format. Is there an alternative to `file_get_contents($response)` that would work here? – MiPlayer123 Nov 17 '21 at 18:24
  • @MiPlayer123 why are you trying to execute `file_get_contents` on response? I don't see any point of doing that. – aso Nov 18 '21 at 20:44