PHP examples

Hi.
Can you include PHP examples as you do with Objective-C ?
Taking in count that PHP is the most used web language, it makes sence to have it.
Thanks

Comments

  • AmosDuveenAmosDuveen Member, Administrator, Moderator admin

    Hi @fkrum56,

    We'll certainly pass your feedback on. Meanwhile, you can always find commercial and even open source software that generates snippets of code for specific tasks in all sorts of languages. For example, I have a tool called Postman which I use to make API calls so that I can reproduce issues people are asking about on this forum; that same tool can also produce snippets of example code for the call in question. Here is the example code for an entry call (ace) using the PHP HttpRequest option:

        <?php
    
        $request = new HttpRequest();
        $request->setUrl('https://od-api.oxforddictionaries.com:443/api/v1/entries/en/ace');
        $request->setMethod(HTTP_METH_GET);
    
        $request->setHeaders(array(
          'postman-token' => 'a6ea9e58-c0e0-0f4e-f397-ddba4c69ee80',
          'cache-control' => 'no-cache',
          'app_key' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
          'app_id' => 'XXXXXXXX'
        ));
    
        try {
          $response = $request->send();
    
          echo $response->getBody();
        } catch (HttpException $ex) {
          echo $ex;
        }
    
  • xeniumxenium Member

    HI there, please provide an example in CURL, the follwin g is not workign

    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'app_id: xxxx',
    'app_key: xxxx'
    ));

  • SimoneSimone Administrator admin

    Hi @xenium
    Have you tried the tool my former colleague mentioned above, Postman?
    I understand it is a free code-snippet generator, so you might be able to get the example you need.

  • xeniumxenium Member

    hi Simone, thanks for your reply. I have briefly tried Postman but not had much luck yet, I was hoping to avoid having to learn how to use it just to get a few lines of code, but if I can find the time I will do a tutorial on it and then hopefully extract some working code.

    Meanwhile if anyone has an example PHP script for a call to Oxford Dictionaries API that would be amazing!

    Thanks again

  • xeniumxenium Member

    Ok a bit of googling + Postman later here is some PHP, seems to work fine:

    $app_key = 'yourappkey';
    $app_id = 'yourappid';

    $auth = array("app_key: $app_key", "app_id: $app_id");

    $url = "https://od-api.oxforddictionaries.com:443/api/v2";
    $endpoint = "entries";
    $language_code = "en-us";
    $lookup_word = "simple";

    $curl = curl_init();

    curl_setopt_array($curl, array(
    CURLOPT_URL => "$url/$endpoint/$language_code/$lookup_word",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => "",
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 0,
    CURLOPT_FOLLOWLOCATION => true,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => "GET",
    CURLOPT_HTTPHEADER => $auth,
    ));

    $response = curl_exec($curl);

    curl_close($curl);
    echo $response;

  • SimoneSimone Administrator admin

    Hi @xenium
    I'm glad it worked in the end - and many thanks for sharing the code!

Sign In or Register to comment.