Skip to content

Instantly share code, notes, and snippets.

@littlebutty
Last active February 13, 2019 18:11
Show Gist options
  • Save littlebutty/ded6640b6a4a32b4d008534b814ee11d to your computer and use it in GitHub Desktop.
Save littlebutty/ded6640b6a4a32b4d008534b814ee11d to your computer and use it in GitHub Desktop.
<?php
/**
* Script that takes in a CSV file of dealer
* contact info and does a web search to try and find
* a phone number, contact link and/or webpage.
*/
/****************************
* SETTINGS
****************************/
$apiEndPoint = 'https://api.cognitive.microsoft.com/bing/v7.0/search'; // Bing Search Endpoint (often requires Azure instance setup)
$acctKey1 = '*fc8ee524a0e40398c5*************';
$acctKey2 = '*f1d0fa12a7441b89b5*************';
$limit = 3; // Home many search results to pull
$fileHasHeaderRow = true; // If the CSV file coming in has a header row or now.
$inFilePath = "/Users/Downloads/file.csv"; // Path to the CSV file to read in.
/****************************/
/**
* Script Execution
*/
if (!is_file($inFilePath)) {
echo "\nThe File your specified does not exist.\n\n";
exit;
}
$inFileName = basename($inFilePath);
$outFileName = $inFileName . "_processed" . ".csv";
$pathInfo = pathinfo($inFilePath);
$inDirName = $pathInfo['dirname'];
$outFilePath = $inDirName . DIRECTORY_SEPARATOR . $outFileName;
$recFileIn = fopen($inFilePath, 'r');
$recFileOut = fopen($outFilePath, 'w+');
$row = 0;
echo "\n";
while (($dealerRow = fgetcsv($recFileIn, 1000, ",")) !== FALSE) {
echo ".";
$row++;
if ($row == 1 && $fileHasHeaderRow) {
array_push($dealerRow, 'Dealer Phone');
array_push($dealerRow, 'Dealer Website');
array_push($dealerRow, 'Dealer Contact URL');
fputcsv($recFileOut, $dealerRow);
continue;
}
$colCount = count($dealerRow);
$dealerName = $dealerRow[2];
$dealerCity = $dealerRow[5];
$dealerState = $dealerRow[6];
$dealerPhone = "";
$dealerURL = "";
$dealerContactURL = "";
if (strlen($dealerName) <= 3) {
echo "\n Skipped Row " . $row . " because a bad dealer name of (" . $dealerName . ").\n";
continue;
}
$query = $dealerName . " in " . $dealerCity . ", " . $dealerState;
$queryString = urlencode("{$query}");
/**
* Make the API call to Bing Search
*/
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.cognitive.microsoft.com/bing/v7.0/search?cc=US&count=3&q=" . $queryString,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Ocp-Apim-Subscription-Key: dfc8ee524a0e40398************",
"Postman-Token: 97e0723b-c8e7-49af-9628-d6b446089c0a",
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
exit;
}
$result = json_decode($response, true);
/**
* Grab the Website at a minimum
*/
if ($result['webPages']) {
$firstSite = $result['webPages']["value"][0];
$dealerURL = $firstSite['url'];
}
/**
* Look for a Phone Number
*/
if (isset($result['places'])) {
$place = $result['places']['value'][0];
if (is_array($place) && array_key_exists('telephone', $place)) {
// BINGO!
$dealerPhone = $place['telephone'];
}
}
/**
* Look ofr a Contact URL
*/
if ($result['webPages']) {
$firstSite = $result['webPages']["value"][0];
if (isset($firstSite['deepLinks'])) {
$deepLinks = $firstSite['deepLinks'];
foreach ($deepLinks as $deepLink) {
if (stripos($deepLink['name'], 'contact') !== FALSE) {
//WooHoo found a Contact URL
$dealerContactURL = $deepLink['url'];
break;
}
}
}
}
/**
* Write out the results
*/
array_push($dealerRow, $dealerPhone);
array_push($dealerRow, $dealerURL);
array_push($dealerRow, $dealerContactURL);
if (fputcsv($recFileOut, $dealerRow) === FALSE) {
echo "Failed to write line to out file for dealer: " . $dealerName;
continue;
}
sleep(1); // Free tier of Bing has throttling
}
echo "\n";
fclose($recFileIn);
fclose($recFileOut);
echo "\nProcessed " . $row . " rows of data,\n\n";
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment