0

I implemented the following API call:

$secret = 'my_secret_key';
# Required parameters
$params = array(
    'unique_user_id'=> $my_nickname,
    'date_of_birth'=> $my_dob,
    'email'=> $my_email,
    'gender'=> $my_gender,
    'zip_code'=> $my_zip,
    'ip_address'=> $my_ip,
    'limit'=> 3,
    'basic'=> 1,
);

$params = http_build_query($params);

$opts = array(
  'http'=> array(
    'method'=> "GET",
    'header'=> "X-Api-Key: $secret",
    'ignore_errors' => true,
  )
);

$url = 'https://www.target-server.com/suppliers_api/works/user';
$url = $url . '?' . $params;


$context = stream_context_create($opts);
/* Sends an http request with additional headers shown above */
$fp = fopen($url, 'r', false, $context);
# Output all data from the response
fpassthru($fp);
fclose($fp);
?>

I got the following output:

{ "messages": [], "status": "success", "surveys": [ { "project_id": 12345678, "name": "Study", "study_type": 1, "cpi": "0.42", "remaining_completes": 104, "conversion_rate": "0.00", "loi": 15, "country": "US", "survey_groups_ids": [], "platform_types": [ "desktop", "ios_tablet", "android_phone", "ios_phone", "android_tablet", "android_kindle", "ios_tablet" ], "match_to_qualify": true, "delay_crediting": false, "tentative_payout": false, "order": { "loi": 15, "ir": 85 }, "is_pmp": false, "entry_link": "https://www.target-servet.com?si=XXX&ssi=SUBID&unique_user_id=user4&hmac=checksum_calculated&offer_id=12345678", "score": null } ], "user_account_status": "good", "quality_score_status": "good" }

I would like PHP to extract the following fields and format them as follows:

Project ID: 12345678
URL: https://www.target-server.com?si=XXX&ssi=SUBID&unique_user_id=user4&hmac=checksum_calculated&offer_id=12345678
CPI: 0.42
LOI: 15

Will you please help me to edit my PHP code in order to select ONLY CERTAIN FIELDS in certain format? Need only fields "entry_link", "project_id", "cpi", "loi".

  • Run `json_decode` on it: https://3v4l.org/v2ZXj – Chris Haas Dec 07 '21 at 18:06
  • Hi Chris! I know about json_decode. How to save output dump to $data variable you indicated? It supposed to work within API response without any any interactions... – Sergey Russkikh Dec 07 '21 at 18:19
  • 1
    You could either use output buffering which is a little hacky IMHO, or maybe swap `fopen` for `file_get_contents` which takes [similar parameters](https://stackoverflow.com/a/48101312/231316) but returns a result – Chris Haas Dec 07 '21 at 18:23
  • Hi Chris! Yes, there is an `fopen` usage in my example API call. How will I extract just few fields I need and format them to be user-freindly? – Sergey Russkikh Dec 07 '21 at 18:28
  • You can't use `fpassthru` if you want to capture data easily, as was commented an hour ago. Use `file_get_contents`. – miken32 Dec 07 '21 at 19:31
  • Hello! I managed `file_get_contents` to work: `$json = file_get_contents($url, false, $context);` instead of `$json = file_get_contents($url, 'r', false, $context);`. It works without `r`, wil anyone explain it? – Sergey Russkikh Dec 08 '21 at 16:14

0 Answers0