0

I need to connect to the Outbrain API (This is the documentation: http://docs.amplifyv01.apiary.io/#). There's a minor example in there, but when I tried connecting to my own account I didn't manage to do so... Can't understand if I put the wrong CURLOPT_URL or didn't write my credentials in the right form... This is my code:

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.outbrain.com/amplify/v0.1/login");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Authorization: BASIC BASE-64-ENC(USERNAME:PASSWORD)",
    "OB-TOKEN-V1: MY_ACCESS_TOKEN"
));
$response = curl_exec($ch);
curl_close($ch);

var_dump($response);

If anyone knows why it didn't worked - I'd very much appreciate it... Also if anyone has an additional code for talking with the Outbrain API - It'll help me a lot. Thank you!

Bramat
  • 979
  • 4
  • 24
  • 40
  • Does the API even respond? With which error? How are you encoding the "USERNAME:PASSWORD" string? – Arnauld Jul 03 '16 at 09:50
  • The response for "var_dump($response)" is "bool(false)", and I write it as it is - with no " or ' – Bramat Jul 03 '16 at 11:29
  • Could you try to insert a `echo curl_error($ch);` before the `curl_close`? – Arnauld Jul 03 '16 at 11:51
  • @Arnauld This is what I get: "SSL certificate problem: unable to get local issuer certificate" – Bramat Jul 03 '16 at 12:58
  • I suppose you should look for similar questions about this error, [like this one.](http://stackoverflow.com/questions/17478283/paypal-access-ssl-certificate-unable-to-get-local-issuer-certificate) – Arnauld Jul 03 '16 at 14:01

1 Answers1

1
<?php

$outbrain_user = 'xxx'; 
$outbrain_pass = 'xxx';

// Basic access authentication
$enc_credentials = base64_encode($outbrain_user.':'.$outbrain_pass);

$ba_authenticacion = 'Basic '.$enc_credentials;

$auth_header = array(
    'Authorization: '.$ba_authenticacion
);

$outbrain_api_endpoint = 'https://api.outbrain.com/amplify/v0.1/';

// authentication
$auth_url = $outbrain_api_endpoint.'/login';

$curl = curl_init();
curl_setopt_array($curl,
  array(
    CURLOPT_URL => $auth_url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => $auth_header
  )
);

$json_access_token = curl_exec($curl);

// parsing json object to string
$token_object = json_decode($json_access_token);
$token_array = get_object_vars($token_object);

// api access_token
$access_token = $token_array['OB-TOKEN-V1'];

Basically you got a wrong syntax while parsing CURLOPT_HTTPHEADER array, also outbrain uses a basic access authentication, you can check here for docs https://en.wikipedia.org/wiki/Basic_access_authentication. With this code you can return the access_token from outbrain.

xiscodev
  • 96
  • 1
  • 1
  • 5