MBlack
MBlack

Reputation: 15

Php and connecting to an API

This may be a very basic question, as I am fairly new to php programming. Hopefully I can learn something about my particular issue.

I am trying to build a simple web application with php that will interface with the BBYOpen API provided by Best Buy. The application need to be able to accept a zipcode and distance radius in order to retrieve Best Buy store locations within the parameters.

Now BBYOpen provides queries for developers, which I have managed to find the one I would need to use.

My problem, being new to php, is:

  1. How exactly can I build a simple HTML page that will accept user input (in the form of a zipcode and distance) and tie that into a php script that can communicate with BBYOpen to retrieve the data.

  2. BBYOpen provides extensive documentation for querying and the information that I can feasible return, but I am lost on how to get my php script to connect to BBYOpen. I know that in order to connect I need to utilize their Remix web service, but I am also at a loss at how I would go about doing that.

Any help or solutions would be appreciated, even in a generic code with comments.

Cheers

Upvotes: 1

Views: 253

Answers (1)

Burhan Çetin
Burhan Çetin

Reputation: 676

    $ch = curl_init('http://api.remix.bestbuy.com/v1/products?apiKey=apiKey&format=json');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',)
    );

    $result = curl_exec($ch);
    $header_size = curl_getinfo($ch,CURLINFO_HEADER_SIZE);
    $header = substr($result, 0, $header_size);
    $body = substr( $result, $header_size );
    curl_close($ch);

    echo "<pre>";
    print_r(json_decode($body));

end wtih this you can get the data;

$json = file_get_contents('php://input');
$data = json_decode($json, true);

Upvotes: 1

Related Questions