batch/1.1/old/getSiteWeb.php
2013-06-19 08:24:49 +00:00

1253 lines
47 KiB
PHP
Raw Blame History

#!/usr/bin/php -c/var/www/batch/config/php_batch_sd.ini
<?php
include_once(FWK_PATH.'common/chiffres.php');
include_once(FWK_PATH.'common/dates.php');
include_once(INCLUDE_PATH.'insee/classMInsee.php');
include_once(INCLUDE_PATH.'partenaires/classMGreffes.php');
include_once(INCLUDE_PATH.'partenaires/classMWeb.php');
include_once(FWK_PATH.'mail/sendMail.php');
/*
$dir='/home/data/afnic/all/';
$iDb=new WDB('sdv1');
// Ouvre un dossier bien connu, et liste tous les fichiers
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
$iFic=0;;
while (($file = readdir($dh)) !== false) {
if ($file <>'.' && $file <>'..') {
$iFic++;
$nomComplet=$dir.$file;
$tabLignes=file($nomComplet);
foreach ($tabLignes as $iLigne=>$ligne) {
if ($iLigne==0) { $ligne1=$ligne; continue; }
elseif ($iLigne==1) { $ligne2=$ligne; continue; }
$tabTmp=explode('|', $ligne);
if (count($tabTmp)>0) {
$tabInsert=array('domaine'=>$tabTmp[0],
'deposant'=>$tabTmp[1],
'fichier'=>$file,
'dateDepot'=>substr($file,0,10),
'ligne1'=>$ligne1,
'ligne2'=>$ligne2,
);
$iDb->insert('domaines', $tabInsert);
}
}
$nbLignes=count($tabLignes);
echo "fichier $iFic, '$nomComplet' : $nbLignes.".EOL;
}
}
closedir($dh);
}
}
die();
/*
$iInsee=new MInsee();
echo $iInsee->valideSiren(518221618);
die();
echo '1&1 = '.(1 & 1).EOL;
echo '1|1 = '.(1 | 1).EOL;
echo '0&1 = '.(0 & 1).EOL;
echo '0|1 = '.(0 | 1).EOL;
echo 'non 1&1 = '.!(1 & 1).EOL;
echo 'non 1|1 = '.!(1 | 1).EOL;
echo 'non 0&1 = '.!(0 & 1).EOL;
echo 'non 0|1 = '.!(0 | 1).EOL;
die();
/*
$adresseNum=26;
$adresseBtq='';
$adresseVoie='RUE';
$adresseRue='DES RIGOLES';
$adresseComp='';
$cp=75020;
$ville='PARIS';
$iInsee=new MInsee();
print_r($iInsee->infoAdresseDom($adresseNum, $adresseBtq, $adresseVoie, $adresseRue, $adresseComp, $cp, $ville));
die();
*/
$iWeb=new MSitesWeb();
//print_r($iWeb->whois('scores-decisions.com'));
//die();
echo date('Y/m/d H:i:s')." - D<>but de la mise <20> jour...".EOL;
$iDb=new WDB('jo');
$iDb2=new WDB('sdv1');
$nbDomaines=$iDb->select('jo.infos_entrep', 'siren, web',"web<>''", false,MYSQL_ASSOC,true);
/*$iDb3=new WDB('sdv1');
$nb=$iDb2->select('domaines', 'id, domaine, deposant, ligne1, ligne2',"1 LIMIT 1027300,200000", false,MYSQL_ASSOC, true);
while ($tabFr=$iDb2->fetch(MYSQL_ASSOC)) {
$i++;
$iDb3->update('domaines', array('domaine' => trim($tabFr['domaine']),
'deposant' => trim($tabFr['deposant']),
'ligne1' => trim($tabFr['ligne1']),
'ligne2' => trim($tabFr['ligne2']),
),
"id=".$tabFr['id'], false);
echo "$i/$nb : ".$tabFr['domaine']."\t".$tabFr['deposant'].EOL;
//die();
}
die('ok');
*/
echo date('Y/m/d H:i:s')." - Il y a $nbDomaines sites Web !".EOL;
while ($tabDomaines=$iDb->fetch(MYSQL_ASSOC)) {
$url=preg_replace('/^http\/\//', 'http://', $tabDomaines['web']);
if (strpos($url, 'http')===false) $url='http://'.$url;
$tabInsert=$iWeb->getInfosSiteWeb($url);
$tabInsert['dateInsert']=date('YmdHis');
print_r($tabInsert);
// Exploration du site si domaine et site/location valide !
// ... avec recherche r<>cursive dans les pages du domaine ok
// Recherche des mentions l<>gales, tel, fax, mel et dirigeants op<6F>rationnels
// Validation Whois ?
$domaine=$tabInsert['domaine'];
$ext=strtolower(getFileExtension($domaine));
if ($ext=='fr') {
$tabFr=$iDb2->select('domaines', 'id, domaine, deposant, typeDeposant, sirenDeposant, sirenValide',"domaine IN ('$domaine', '$domaine ') OR domaine LIKE '$domaine %'", false,MYSQL_ASSOC);
print_r($tabFr);
if (count($tabFr)==0 || $tabFr[0]['typeDeposant']=='PM') {
$tabAfnic=$iWeb->getInfosAfnic($domaine);
$tabInsert['siren']=@$tabAfnic['siren']*1;
$tabInsert['marquefr']=@$tabAfnic['marquefr'];
randsleep(21,60);
/* if ($siren>10000) {
$iDb2->update('domaines', array('sirenDeposant'=>$siren, 'sirenValide'=>'2'), "id=$id");
echo " === VALIDATION par l'AFNIC ===";
}*/
}
//die
}
//else die("'$ext'");
if (!$iDb2->insert('sitesWeb', $tabInsert, false)) {
if (mysql_errno()==1062) {
unset($tabInsert['dateInsert']);
if (!$iDb2->update('sitesWeb', $tabInsert, "url='".$tabInsert['url']."'", false)) {
echo mysql_errno().' : '.mysql_error().EOL;
die();
}
}
}
}
die();
$iDb=new WDB('sdv1');
$iDb2=new WDB('sdv1');
$iInsee=new MInsee();
$idDeb=@file_get_contents('/tmp/idSiteWeb.loc')*1;
$nbDomaines=$iDb->select('domaines', 'id, domaine, deposant, typeDeposant, sirenDeposant, sirenValide',"sirenDeposant<1000 AND id>$idDeb AND id>5829", false,MYSQL_ASSOC,true);
//675, 678
echo date('Y/m/d H:i:s')." - Il y a $nbDomaines <20> mettre <20> jour depuis $idDeb !".EOL;
while ($tabDomaines=$iDb->fetch(MYSQL_ASSOC)) {
$id=$tabDomaines['id'];
$domaine=trim($tabDomaines['domaine']);
$urlBase='http://www.'.$domaine;
$tabUrls=crawler($urlBase);
echo date('Y/m/d H:i:s')." - $id, ".$tabDomaines['domaine'].' : '.$urlBase.EOL;
print_r($tabUrls);
foreach ($tabUrls as $i=>$url) {
$ext=strtolower(getFileExtension($url));
echo date('Y/m/d H:i:s')." - $id, $domaine, $i, $url [$ext] : ";
if ($ext=='jpg' || $ext=='gif' || $ext=='jpeg' || $ext=='png' || $ext=='bmp' || $ext=='tif' || $ext=='tiff') {
echo "(images ignor<6F>es)".EOL;
continue;
} elseif ($ext=='css' || $ext=='js' || $ext=='swf' || $ext=='jar') {
echo "(codes ignor<6F>es)".EOL;
continue;
} elseif (strpos($url, $domaine)===false) {
echo "(hors domaine ignor<6F>)".EOL;
continue;
}
$page=getUrl($url, '', '', '', false, '', '', 5);
$body=$page['body'];
$dom2p=strtr($domaine,array('.'=>'\.','-'=>'\-',));
if (preg_match_all('/([a-z0-9\-\._]{1,80}@'.$dom2p.'\s)/Ui', $body, $matches)) {
$tabMails=$matches[1];
print_r($tabMails);
}
$typeDeposant=$tabDomaines['typeDeposant'];
$idOk=false;
if (preg_match_all("/((?:[0-9]{9,9})|(?:[0-9]{3,3} [0-9]{3,3} [0-9]{3,3})|(?:[0-9]{3,3}\.[0-9]{3,3}\.[0-9]{3,3})|(?:[0-9]{3,3}-[0-9]{3,3}-[0-9]{3,3}))/", $body, $matches)) {
foreach ($matches[1] as $siren) {
$siren=strtr($siren, array('.'=>'','-'=>'', ' '=>''));
$nom=$nom2=$sigle=$enseigne='';
$levNom=$pct=$simNom=0;
if ($iInsee->valideSiren($siren)) {
echo "Siren trouv<75> : $siren pour ".$tabDomaines['deposant'].EOL;
$nomDeposant=strtoupper($tabDomaines['deposant']);
$tabId=@$iInsee->getIdentiteEntreprise($siren);
if (@$tabId['Nom']=='') continue;
$nom=$tabId['Nom'];
$nom2=$tabId['Nom2'];
$sigle=$tabId['Sigle'];
$enseigne=$tabId['Enseigne'];
if ($typeDeposant=='PM') {
$levNom=@levenshtein($nomDeposant,$nom);
$simNom=@similar_text($nomDeposant,$nom,$pct);
} elseif ($nom2<>'') {
$domaineSansFr=strtoupper(substr($domaine,0,strlen($domaine)-3));
//die($domaineSansFr);
$levNom1=@levenshtein($nomDeposant,$nom2);
$simNom1=@similar_text($nomDeposant,$nom2,$pct1);
$levNom2=@levenshtein($domaineSansFr,$nom2);
$simNom2=@similar_text($domaineSansFr,$nom2,$pct2);
if ($levNom2<$levNom1) $levNom=$levNom2;
else $levNom=$levNom1;
if ($simNom2<$simNom1) $simNom=$simNom2;
else $simNom=$simNom1;
if ($pct2<$pct1) $pct=$pct2;
else $pct=$pct1;
} elseif ($sigle<>'') {
$levNom=@levenshtein($nomDeposant,$sigle);
$simNom=@similar_text($nomDeposant,$sigle,$pct);
}
echo "$siren, $nom, $nom2, $sigle, $enseigne : $levNom,$simNom,$pct,$levNom1,$simNom1,$pct1,$levNom2,$simNom2,$pct2".EOL;
if ($levNom>0 && $levNom<15 && $pct>44 && $siren>10000) {
//, domaine, deposant, typeDeposant, ',"<1000", false,MYSQL_ASSOC,true
$iDb2->update('domaines', array('sirenDeposant'=>$siren, 'sirenValide'=>'1'), "id=$id");
echo " === VALIDATION par les mentions l<>gales ===";
$idOk=true;
//die();
}
}
}
}
if (!$idOk && $typeDeposant=='PM') {
$tabAfnic=$iWeb->getInfosAfnic($domaine);
$siren=@$tabAfnic['siren']*1;
if ($siren>10000) {
$iDb2->update('domaines', array('sirenDeposant'=>$siren, 'sirenValide'=>'2'), "id=$id");
echo " === VALIDATION par l'AFNIC ===";
}
}
echo EOL;
}
file_put_contents('/tmp/idSiteWeb.loc', $id);//file_get_contents($file);
}
die();
// boucler sur l'ensemble des liens qui ont le m<>me domaine de base
// rechercher les pages mentions l<>gales, ou avec siren, reg ex siren, emails,
print_r(@$iWeb->chercheWebParNom('SCORES DECISIONS'));
//print_r($page);
die();
try
{
// Enter the Application ID, in quotation marks, supplied by the
// Developer Provisioning System as the value of the AppID on the SearchRequest.
$appId = '56D6CBA671C986D3EA11B1B48F97507BC5A00D51';
// Enter the Url to the Wsdl file.
$wsdl = "http://soap.search.msn.com/webservices.asmx?wsdl";
$sortBy = 'Default';
$numResults = 20; /* default the value to 10, this is also the default for the service if you do not provide a value. */
$numOffset = 0; /* default the value to 0, this is also the default for the service is you do not provide a value. */
$searchQuery = 'SCORES ET DECISIONS';
$nearMe = false;
$safeSearch = 'Moderate'; /* default value to Moderate, this is also the default for the service if you do not provide a value. */
$searchFlags = 0;
$flags = 'None';
$cultureInfo = 'fr-FR'; /* default value to United States, English */
/* 'en-US'
'en-GB'
'da-DK'
'de-AT'
'de-CH'
'de-DE'
'en-AU'
'en-CA'
'en-GB'
'en-ID'
'en-IE'
'en-IN'
'en-MY'
'en-NZ'
'en-PH'
'en-SG'
'en-US'
'en-XA'
'en-ZA'
'es-AR'
'es-ES'
'es-MX'
'es-US'
'es-XL'
'fi-FI'
'fr-BE'
'fr-CA'
'fr-CH'
'fr-FR'
'it-IT'
'ja-JP'
'ko-KR'
'nb-NO'
'nl-BE'
'nl-NL'
'pt-BR'
'sv-SE'
'tr-TR'
'zh-CN'
'zh-HK'
'zh-TW'
*/
$latitude = 47.422433;
$longitude = -122.305833;
$radius = 5.0;
$webSearchTags = '';
$newsSearchTags = '';
$docType = 'ANYFILE';
$sourceType = '';
$markQueryWords = '';
$disableSpellCorrectForSpecialWords = '';
$disableHostCollapsing = '';
/* Use the Request object to get the search options from the web page. */
if (!empty($_REQUEST['searchQuery']))
{
$searchQuery = stripslashes($_REQUEST['searchQuery']);
}
if (!empty($_REQUEST['nResults']) AND is_numeric($_REQUEST['nResults']))
{
$numResults = stripslashes($_REQUEST['nResults']);
}
if (!empty($_REQUEST['offset']) AND is_numeric($_REQUEST['offset']))
{
$numOffset = stripslashes($_REQUEST['offset']);
}
if (!empty($_REQUEST['nearMeButton']))
{
$nearMe = true;
}
// Check the user selection for SafeSearchOptions.
// The default setting is Moderate.
if (!empty($_REQUEST['safeGroup']))
{
$safeSearch = stripslashes($_REQUEST['safeGroup']);
}
// Determine the user's CultureInfo selection.
// The following code gets the CultureInfo value from the combo box.
// The combo box collection contains all values recognized by the Windows Live Search Engine.
// You can also set the CultureInfo to a specific value in the code, as shown in the
// following example:
// searchRequest->CultureInfo = 'fr-CA';
if (!empty($_REQUEST['cultureInfo']))
{
$cultureInfo = stripslashes($_REQUEST['cultureInfo']);
}
if (!empty($_REQUEST['longitude']) AND is_numeric($_REQUEST['longitude']))
{
$longitude = $_REQUEST['longitude'];
}
if (!empty($_REQUEST['latitude']) AND is_numeric($_REQUEST['latitude']))
{
$latitude = $_REQUEST['latitude'];
}
if (!empty($_REQUEST['sRadius']) AND is_numeric($_REQUEST['sRadius']))
{
$radius = $_REQUEST['sRadius'];
}
if (!empty($_REQUEST['webSearchTags']))
{
$webSearchTags = stripslashes($_REQUEST['webSearchTags']);
}
if (!empty($_REQUEST['newsSearchTags']))
{
$newsSearchTags = stripslashes($_REQUEST['newsSearchTags']);
}
if (!empty($_REQUEST['fileTypeGroup']))
{
$docType = stripslashes($_REQUEST['fileTypeGroup']);
}
if (!empty($_REQUEST['sortGroup']))
{
$sortBy = stripslashes($_REQUEST['sortGroup']);
}
if (!empty($_REQUEST['phoneBookSourceType']))
{
$sourceType = stripslashes($_REQUEST['phoneBookSourceType']);
}
if (!empty($_REQUEST['MarkQueryWords']))
{
$markQueryWords = stripslashes($_REQUEST['MarkQueryWords']);
}
if (!empty($_REQUEST['DisableSpellCorrectForSpecialWords']))
{
$disableSpellCorrectForSpecialWords = stripslashes($_REQUEST['DisableSpellCorrectForSpecialWords']);
}
if (!empty($_REQUEST['DisableHostCollapsing']))
{
$disableHostCollapsing = stripslashes($_REQUEST['DisableHostCollapsing']);
}
// Create an options array that includes the mapping of the local classes
// to the server classes. Notice that we map the Service DateTime class to SoapDateTime
// because PHP already has a DateTime class that conficts with the Service Class.
$options = array('trace'=>1,
'classmap' => array('Address' => 'Address' /*mapped*/,
'Image' => 'Image',
'SearchTag' => 'SearchTag' /*mapped*/,
'Result' => 'Result' /*mapped*/,
'Location' => 'Location' /*mapped*/,
'DateTime' => 'SoapDateTime' /*mapped*/,
'SearchConstants' => 'SearchConstants' /*mapped*/,
'SourceRequest' => 'SourceRequest' /*mapped*/,
'ArrayOfstringSearchTagFilters' => 'ArrayOfstringSearchTagFilters' /*mapped*/,
'ArrayOfSearchTagSearchTagsArray' => 'ArrayOfSearchTagSearchTagsArray' /*mapped*/,
'SourceResponse' => 'SourceResponse' /*mapped*/,
'ArrayOfResultResults' => 'ArrayOfResultResults' /*mapped*/,
'SearchRequest' => 'SearchRequest' /*mapped*/,
'ArrayOfSourceRequestRequests' => 'ArrayOfSourceRequestRequests' /*mapped*/,
'SearchResponse' => 'SearchResponse' /*mapped*/,
'ArrayOfSourceResponseResponses' => 'ArrayOfSourceResponseResponses' /*mapped*/,
'Search' => 'Search' /*mapped*/));
// Create a soap client passing in the Url for the server and the options array.
$soapClient = new SoapClient($wsdl, $options);
// Define your structures to use for creating the parameters for the request and the results
// upon return.
class Search
{
public $Request;
}
class Image
{
public $ImageURL;
public $ImageWidth;
public $ImageHeight;
public $ImageFileSize;
public $ThumbnailURL;
public $ThumbnailWidth;
public $ThumbnailHeight;
public $ThumbnailFileSize;
}
class SearchConstants
{
public $MarkBegin;
public $MarkEnd;
}
class SearchRequest
{
public $AppID;
public $Query;
public $CultureInfo;
public $SafeSearch;
public $Flags;
public $Location;
public $Requests;
}
class Location
{
public $Latitude;
public $Longitude;
public $Radius;
}
class Address
{
public $AddressLine;
public $PrimaryCity;
public $SecondaryCity;
public $Subdivision;
public $PostalCode;
public $CountryRegion;
public $FormattedAddress;
}
class SoapDateTime
{
public $Year;
public $Month;
public $Day;
public $Hour;
public $Minute;
public $Second;
}
class SearchTag
{
public $Name;
public $Value;
}
class SourceRequest
{
public $Source;
public $Offset;
public $Count;
public $FileType;
public $SortBy;
public $ResultFields;
public $SearchTagFilters;
}
class ArrayOfstringSearchTagFilters
{
public $string;
}
class Result
{
public $Title;
public $Description;
public $Url;
public $DisplayUrl;
public $CacheUrl;
public $Source;
public $SearchTags;
public $Phone;
public $DateTime;
public $Address;
public $Location;
public $SearchTagsArray;
public $Summary;
public $ResultType;
}
class ArrayOfSearchTagSearchTagsArray
{
public $SearchTag;
}
class SourceResponse
{
public $Source;
public $Offset;
public $Total;
public $Results;
}
class ArrayOfResultResults
{
public $Result;
}
class ArrayOfSourceRequestRequests
{
public $SourceRequest;
}
class SearchResponse
{
public $Responses;
}
class ArrayOfSourceResponseResponses
{
public $SourceResponse;
}
// Each SourceRequest can be configured to return one or more common result fields:
// Title, Description, Url, DisplayUrl, CacheUrl, Source, SearchTags, Phone, Address, Location;
// The fields return vary by SourceType. Extended data fields can also be returned for specific
// SourceTypes; see the online documentation for details.
// Fields can be combined using by space delimiting the field names;
// All returns all available fields in the enumeration.
// Note that some SourceRequest objects cannot return all of the fields
// described. For example, a SourceRequest of type Spelling returns one spelling
// suggestion in the Title field, so the returned data is the same when the
// ResultFields property is set to ResultFieldMask.All or ResultFieldMask.Title.
// The first SourceRequest is a Web source request. This can be a request for Web or
// Web-Local results, depending on whether the Location object is set. When the user clicks
// Near Me, the sample application applies the Location information to the search and
// returns Web-Local results (results that consider Location as a relevance factor) and
// PhoneBook results.
$sourceRequest1 = new SourceRequest();
$sourceRequest1->Source = 'Web';
// Check for the number of results to return per page. If this value is not set,
// the service returns the default number of results, which is ten per page.
$sourceRequest1->Count = $numResults;
// Check the offset value. The default offset is zero, meaning that results are
// returned from the Windows Live Engine with the highest-ranking result displayed first.
// The Offset property is most commonly used in conjunction with the Count
// property, above, for paging through long lists of results.
$sourceRequest1->Offset = $numOffset;
// If the user choses to specify a SearchTagFilter or filters, the SearchTags returned will contain
// only those specified. Example:
// Query: MSDN, SearchTagFilter: search.title;search.mscategory
if (!empty($webSearchTags))
{
$sourceRequest1->SearchTagFilters = explode(';', $webSearchTags);
}
// Determine the user's choice of FileType. The default setting is any file
// type. To restrict the FileType, set the SourceRequest.FileType to a specific string
// value as in the following settings for the Microsoft Word .DOC, Autodesk .DWF,
// HTML files with the .HTM or .HTML extension, Adobe .PDF, Microsoft PowerPoint .PPT,
// Adobe PostScript .PS, Microsoft Rich Text Format .RTF, generic text files with the
// .TEXT ot .TXT extension, and Microsoft Excel .XLS types.
if ($docType != 'ANYFILE')
{
$sourceRequest1->FileType = $docType;
}
// For this SourceRequest, we can return all fields...
$sourceRequest1->ResultFields = 'All SearchTagsArray';
// Or just specific fields, such as SearchTags.
//$sourceRequest1->ResultFields = 'SearchTags';
// Or to return a subset of all fields, combine the result fields using the bitwise
// OR operator (|). The following example returns the Title and URL fields only:
//$sourceRequest1->ResultFields = 'Title Url';
// The second SourceRequest is a spelling source request. Spelling returns only one
// result per call, in the Title field.
$sourceRequest2 = new SourceRequest();
$sourceRequest2->Source = 'Spelling';
// Set the Count to 1 and the Offset to 0 for SourceType.Spelling; Spelling returns
// only one result per request (Count = 1) at offset zero (Offset = 0).
$sourceRequest2->Count = 1;
$sourceRequest2->Offset = 0;
$sourceRequest2->SortBy = 'Default';
$sourceRequest2->ResultFields = 'Title';
// The third SourceRequest is a PhoneBook source request. PhoneBook results include
// a title, description, URL, phone number, address object, and location object.
$sourceRequest3 = new SourceRequest();
$sourceRequest3->Source = 'PhoneBook';
$sourceRequest3->Count = $numResults;
$sourceRequest3->Offset = $numOffset;
$sourceRequest3->SortBy = $sortBy;
$sourceRequest3->ResultFields = 'Address Description Location Phone Title Url';
// If a file type was entered, then set the file type
if (!empty($sourceType))
{
$sourceRequest3->FileType = $sourceType;
}
// The fourth SourceRequest is a News source request. News results include
// a title, description, URL, news source, and a date/time object.
$sourceRequest4 = new SourceRequest();
$sourceRequest4->Source = 'News';
$sourceRequest4->Count = $numResults;
$sourceRequest4->Offset = $numOffset;
$sourceRequest4->SortBy = 'Default';
// If the user chose to specify a SearchTagFilter or filters, the SearchTags returned will contain
// only those specified. Example: Query: MSNBC, SearchTagFilter: search.source
if (!empty($newsSearchTags))
{
$sourceRequest4->SearchTagFilters = explode(';', $newsSearchTags);
}
// In this sample, we will return all of the fields, as well as
// the extended news return fields DateTime.
$sourceRequest4->ResultFields = 'All DateTime SearchTagsArray';
// The fifth SourceRequest is a QueryLocation request. The response includes the keyword for the query,
// such as "pizza" and the location name requested, such as a postal code ("98052") or a place name
// ("New York, NY"). The keyword is returned in the QueryLocation.Title field and the place name or
// postal code is returned in the QueryLocation.Description field.
$sourceRequest5 = new SourceRequest();
$sourceRequest5->Source = 'QueryLocation';
$sourceRequest5->ResultFields = 'All';
$sourceRequest5->Count = 10;
$sourceRequest5->Offset = $numOffset;
$sourceRequest5->SortBy = 'Default';
// The sixth SourceRequest is an InlineAnswers request. Inline Answers are returned only to those
// commercial partners who have subscribed to the service(s). Possible types include: Encarta,
// Finance, Weather, and movie ShowTimes. InlineAnswers extended fields include the Summary field,
// which displays a compact version of the answer, suitable for mobile devices such as smart phones,
// and a ResultType field, which returns the name of the InlineAnswer type that responded to the
// query.
$sourceRequest6 = new SourceRequest();
$sourceRequest6->Source = 'InlineAnswers';
$sourceRequest6->ResultFields = 'All ResultType Summary';
$sourceRequest6->Count = 10;
$sourceRequest6->Offset = $numOffset;
// The seventh SourceRequest is an Image request. Image results can include the URL and Display URL
// for the HTML page that contains the image, the image name (in the Title field), the image file size,
// the image height and width, the thumbnail file size, the thumbnail height and width, and URIs for
// the full image and thumbnail.
$sourceRequest7 = new SourceRequest();
$sourceRequest7->Source = 'Image';
$sourceRequest7->ResultFields = 'All Image';
$sourceRequest7->Count = $numResults;
$sourceRequest7->Offset = $numOffset;
$sourceRequest7->SortBy = 'Default';
// For the sample application, we will use seven SourceRequest objects: SourceType.Web,
// which returns Web results or Web-Local results if a Location object is present;
// SourceType.Spelling, which returns spelling suggestions; SourceType.PhoneBook,
// which returns White Pages and Yellow Pages entries and geographic information for
// select markets when a Location object is present; SourceType.News, which returns
// news stories; SourceType.QueryLocation, which returns the keyword and place name
// for a query such as "pizza new york" or "pizza 98052" as "pizza" in the Title field and
// the location name requested such as a postal code ("98052") or a place name ("New York, NY");
// and SourceType.InlineAnswers, which returns Encarta, Finance, Weather, and ShowTimes,
// for commercial partners who have subscribed to these services.
$arrayOfSourceRequestRequests = new ArrayOfSourceRequestRequests();
$arrayOfSourceRequestRequests->SourceRequest[0] = $sourceRequest1;
$arrayOfSourceRequestRequests->SourceRequest[1] = $sourceRequest2;
$arrayOfSourceRequestRequests->SourceRequest[2] = $sourceRequest3;
$arrayOfSourceRequestRequests->SourceRequest[3] = $sourceRequest4;
$arrayOfSourceRequestRequests->SourceRequest[4] = $sourceRequest5;
$arrayOfSourceRequestRequests->SourceRequest[5] = $sourceRequest6;
$arrayOfSourceRequestRequests->SourceRequest[6] = $sourceRequest7;
// Determine whether the user chose to enable query word marking, disable spell correction
// for search operators, and disable host collapsing.
if (!empty($markQueryWords))
{
$searchFlags += 1;
}
if (!empty($disableSpellCorrectForSpecialWords))
{
$searchFlags += 2;
}
if (!empty($disableHostCollapsing))
{
$searchFlags += 4;
}
switch ($searchFlags)
{
case 1:
{
$flags = 'MarkQueryWords';
break;
}
case 2:
{
$flags = 'DisableSpellCorrectForSpecialWords';
break;
}
case 3:
{
$flags = 'MarkQueryWords DisableSpellCorrectForSpecialWords';
break;
}
case 4:
{
$flags = 'DisableHostCollapsing';
break;
}
case 5:
{
$flags = 'MarkQueryWords DisableHostCollapsing';
break;
}
case 6:
{
$flags = 'DisableSpellCorrectForSpecialWords DisableHostCollapsing';
break;
}
case 7:
{
$flags = 'MarkQueryWords DisableSpellCorrectForSpecialWords DisableHostCollapsing';
break;
}
default:
{
$flags = 'None';
break;
}
}
// Create a new SearchRequest object that represents the search request. Each SearchRequest can
// contain multiple SourceRequest objects; however, each SearchRequest can contain
// only one SourceRequest of each type. (More on this later.)
$request = new SearchRequest();
$request->AppID = $appId;
// The Query field of the SearchRequest object is the text of the query submitted to the
// Windows Live Search Engine. The query can contain any valid query text supported by the
// Windows Live Search Engine, including advanced query syntax.
$request->Query = $searchQuery;
$request->CultureInfo = $cultureInfo;
$request->SafeSearch = $safeSearch;
$request->Flags = $flags;
// To return PhoneBook results in addition to Web results, the user must set the Location.
// The following code sets the Latitude and Longitude fields of the Location
// object and the Radius of the search.
// If invalid Location data is specified, the Windows Live Search Engine will attempt to
// return search results based on the information provided. Note that zero results
// may return if the Location data does not map to a valid geographic location.
// The value of Radius also affects the search results; a small value for Radius may
// return fewer results than expected.
// The sample application user interface does not validate user input.
if ($nearMe)
{
$location = new Location();
$location->Longitude = $longitude;
$location->Latitude = $latitude;
$location->Radius = $radius;
$request->Location = $location;
}
// The Requests field of the SearchRequest object is the array of SourceRequest objects
// for this search.
$request->Requests = $arrayOfSourceRequestRequests;
$parameters = new Search();
$parameters->Request = $request;
// Once the SearchRequest has been configured and all of the SourceRequest
// objects have been configured and added to the SearchRequest, the
// application can process the request.
// Execute the search request and return the search response by
// setting the SearchResponse to MSNSearchService->Search(SearchRequest).
$searchResponse = $soapClient->Search($parameters);
$sourceResponse = $searchResponse->Response->Responses->SourceResponse;
if (!empty($searchQuery))
{
// Check the SearchResponse to determine whether it contains a spelling suggestion.
// The spelling suggestion is returned in the Title field of the Spelling SourceResponse.
// If the SearchResponse does not contain a Spelling SourceResponse, there is no
// spelling suggestion and the application passes the SearchResponse to the printResults method.
// If a Spelling SourceResponse is returned, the application displays a message that asks
// the user to choose whether to ignore the spelling suggestion or accept the spelling
// suggestion and re-execute the query with the updated query string.
// To test for the presence of the Spelling SourceResponse, check the Total field for
// the SearchResponse.Response for the Spelling SourceType.
if (is_array($sourceResponse) AND $sourceResponse[1]->Total == 1)
{
HandleSpellingSuggestions($sourceResponse);
}
else
{
PrintResponses($sourceResponse);
}
}
}
// Catch SOAP exceptions.
Catch (SoapFault $exception)
{
print '<br/><br/>'.$exception;
}
// Catch other exceptions
Catch (Exception $ex)
{
print '<br/><br/>'.$ex;
}
/*
This function is called once to iterate the Source Reponse and print the results.
*/
function PrintResponses($sourceResponse)
{
// Using foreach, loop through all the SourceResponses in the SearchResponse.
foreach($sourceResponse as $item)
{
PrintResponse($item);
}
}
/*
This function is called once for each Source Response to print the Response oject. I will iterate through and call PrintResult
to print each result object.
*/
function PrintResponse($itemToPrint)
{
$results = $itemToPrint->Results->Result;
if ($itemToPrint->Total > 0)
{
// We had at least one result, so print out the title and the *estimate* of the total
// number of returned results with the exception of spelling which will return zero
// results or one result.
print '<h3><b>'.$itemToPrint->Source.' - Total Results: '.$itemToPrint->Total.'</h3></b><hr>';
// Check that we got results back
if (!empty($results))
{
// If we have an array, we got back multiple results and we need to loop
// through the results using a foreach, printing each result.
if (is_array($results))
{
foreach($results as $result)
{
PrintResult($itemToPrint->Source, $result);
}
}
else
{
// If we don't have an array, we only had one result so we just need to print it.
PrintResult($itemToPrint->Source, $results);
}
}
}
}
/*
This function gets called for each result in the queryResponse in order to print out
the results. The source parameter is the source query Web News PhoneBook etc....
The resultsToPrint parameter is the Result object that contains the information to print.
*/
function PrintResult($source, $resultsToPrint)
{
GLOBAL $latitude;
GLOBAL $longitude;
// Don't print the lables for results that are not reutnred (NULL) or contains not text.
if (!empty($resultsToPrint->Title))
{
print '<b>Title: </b>'.HighlightKeywords($resultsToPrint->Title).'<br/>';
}
if (!empty($resultsToPrint->Description))
{
print '<b>Description: </b>'.HighlightKeywords($resultsToPrint->Description).'<br/>';
}
if (!empty($resultsToPrint->DisplayUrl))
{
print '<b>DisplayUrl: </b><a href="'.$resultsToPrint->DisplayUrl.'">'.$resultsToPrint->DisplayUrl.'</a><br/>';
}
if (!empty($resultsToPrint->Url))
{
print '<b>Url: </b><a href="'.$resultsToPrint->Url.'">'.$resultsToPrint->Url.'</a><br/>';
}
if (!empty($resultsToPrint->SearchTags))
{
print '<b>SearchTags: </b>'.$resultsToPrint->SearchTags.'<br>';
}
// Check to see if we got a SearchTagsArray back and verify that it is an array.
if (!empty($resultsToPrint->SearchTagsArray->SearchTag) AND is_array($resultsToPrint->SearchTagsArray->SearchTag))
{
// Printout the count of items
print '<b>SearchTagsArray (Length '.count($resultsToPrint->SearchTagsArray->SearchTag).')</b><br><br>';
$counter = 0;
// Iterate through each item printing out the Name and Value
foreach($resultsToPrint->SearchTagsArray->SearchTag as $tag)
{
print '<b>SearchTagsArray['.$counter++.'] Name: </b>'.$tag->Name.'<br>';
print '<b>SearchTagsArray['.$counter++.'] Value: </b>'.$tag->Value.'<br>';
}
}
if (!empty($resultsToPrint->CacheUrl))
{
print '<b>CacheUrl: </b><a href="'.$resultsToPrint->CacheUrl.'">'.$resultsToPrint->CacheUrl.'</a><br/>';
}
if (!empty($resultsToPrint->Source))
{
print '<b>Source: </b>'.$resultsToPrint->Source.'<br/>';
}
if (!empty($resultsToPrint->Summary))
{
print '<b>Summary: </b>'.$resultsToPrint->Summary.'<br/>';
}
if (!empty($resultsToPrint->ResultType))
{
print '<b>ResultType: </b>'.$resultsToPrint->ResultType.'<br/>';
}
if (!empty($resultsToPrint->DateTime))
{
// Make a date from the date results and print the Raw data for the DateTime returned.
$dateOfStory = date('n/j/Y g:i:s A', mktime($resultsToPrint->DateTime->Hour, $resultsToPrint->DateTime->Minute, $resultsToPrint->DateTime->Second, $resultsToPrint->DateTime->Month, $resultsToPrint->DateTime->Day, $resultsToPrint->DateTime->Year));
print '<b>NewsDateTime (Raw Data): </b>'.$dateOfStory.'<br/>';
$age = CalculateAgeOfStory($resultsToPrint->DateTime, 5);
print '<b>NewsDateTime (Age of Story): '.$age.'</b><br/>';
}
// Process PhoneBook results.
// If the search was a "Near Me" search, the Location settings are considered as a relevance factor in the
// Web search and PhoneBook results may also be returned for appropriate queries.
// If the SourceType returned is a PhoneBook SourceType, print a label for the Phone field, each field
// in the Address Object, and each field in the returned Location object.
// Since Address.FormattedAddress is always blank and Location.Radius always returns 5,
// these fields are skipped in the sample.
// The SecondaryCity and PostalCode fields are not returned for addresses in the United States;
// the SecondaryCity field is used for addresses in the United Kingdom.
if ($source == 'PhoneBook')
{
if (!empty($resultsToPrint->Phone))
{
print '<b>Phone: </b>'.$resultsToPrint->Phone.'<br/>';
}
if (!empty($resultsToPrint->Address))
{
$address = $resultsToPrint->Address;
if (!empty($address->AddressLine))
{
print '<b>AddressLine: </b>'.$address->AddressLine.'<br/>';
}
if (!empty($address->CountryRegion))
{
print '<b>CountryRegion: </b>'.$address->CountryRegion.'<br/>';
}
if (!empty($address->PostalCode))
{
print '<b>PostalCode: </b>'.$address->PostalCode.'<br/>';
}
if (!empty($address->PrimaryCity))
{
print '<b>PrimaryCity: </b>'.$address->PrimaryCity.'<br/>';
}
if (!empty($address->SecondaryCity))
{
print '<b>SecondaryCity: </b>'.$address->SecondaryCity.'<br/>';
}
if (!empty($address->Subdivision))
{
print '<b>Subdivision: </b>'.$address->Subdivision.'<br/>';
}
}
if (!empty($resultsToPrint->Location))
{
$location = $resultsToPrint->Location;
print '<b>Latitude: </b>';
print $location->Latitude;
print '<br/><b>Longitude: </b>';
print $location->Longitude;
print '<br/>';
// Calculate the "Great Circle" distance between the Location specified in the SearchRequest
// and the longitude and latitude of each Location returned by the query in miles and kilometers.
//
// Note: The distance is calculated based on the values entered in the Latitude and Longitude
// text boxes in the user interface in the Location Information section of the Settings tab.
// For queries of the form keyword, place name or keyword, ZIP code, this code will not compute
// the distance from the center of the city as returned in the QueryLocation.Location object.
$earthRadius = 3963.0;
$earthRadiusKM = 6392.0;
$latitudeIn = $latitude;
$longitudeIn = $longitude;
$latitudeOut = $location->Latitude;
$longitudeOut = $location->Longitude;
$radianConversion = pi() / 180.0;
$operandOne = cos($radianConversion * (90.0 - $latitudeIn)) * cos($radianConversion * (90.0 - $latitudeOut));
$operandTwo = sin($radianConversion * (90.0 - $latitudeIn)) * sin($radianConversion * (90.0 - $latitudeOut)) * cos($radianConversion * ($longitudeIn - $longitudeOut));
$acosOfSum = acos($operandOne + $operandTwo);
$finalValue = $earthRadius * $acosOfSum;
$finalValueKM = $earthRadiusKM * $acosOfSum;
print '<b>Distance (Miles): </b>'.round($finalValue, 2).'<br/>';
print '<b>Distance (Kilometers): </b>'.round($finalValueKM, 2).'<br/><hr/>';
}
}
// If the query was of the form "keyword(s) Place_Name" or "keyword(s) ZIP_Code"
// and SourceType.QueryLocation was requested, print a string showing the
// QueryLocation result, as on the Live Search website. QueryLocation can be used in conjunction
// with PhoneBook results for select markets, as listed in the online documentation.
// Examples of this type of location-based keywrod query are:
// "pizza new york", "pizza ottawa", and "pizza 98052".
if ($source == 'QueryLocation')
{
if (!empty($resultsToPrint->Location))
{
$location = $resultsToPrint->Location;
print '<b>Sample Return String for QueryLocation:</b><br/>';
print 'Top local listings for <b>'.$resultsToPrint->Title.'</b> near ';
print '<b>'.$resultsToPrint->Description.'</b>';
print ' ('.$location->Longitude.', '.$location->Latitude.')<br/>';
}
}
// If the results is an image, then display the thumbnail and build a link to the full size image.
if ($source == 'Image')
{
$image = $resultsToPrint->Image;
if ($image->ThumbnailFileSize > 0)
{
print '<b>Thumbnail File Size: </b>'.$image->ThumbnailFileSize.'<br/>';
}
if ($image->ThumbnailHeight > 0 AND $image->ThumbnailWidth > 0)
{
print '<b>Thumbnail Height: </b>'.$image->ThumbnailHeight.' <b>Thumbnail Width: </b>'.$image->ThumbnailWidth.'<br/>';
print '<b>Thumbnail:</b><br/><img src="'.$image->ThumbnailURL.'"></a><br/>';
}
if ($image->ImageFileSize > 0)
{
print '<b>Image File Size: </b>'.$image->ImageFileSize.'<br/>';
}
if ($image->ImageHeight > 0 AND $image->ImageWidth > 0)
{
print '<b>Image Height: </b>'.$image->ThumbnailHeight.' <b>Image Width: </b>'.$image->ThumbnailWidth.'<br/>';
print '<b>Full Size Image URL: </b><a href="'.$image->ImageURL.'">'.$image->ImageURL.'</a><br/><hr/>';
}
}
print '<br/>';
}
/*
This function calculates the age of the news story. The first parameter is the raw date of the story which
is in Universal time. The second parameter is the number of hours offset from your local time to universal
time. This function will then calulate the current time based on universal time and figure out the age
of the story. If the story is more than one day old, then the date is returned. If the story is more than
an hour but less than one day, then the number of hours is returned. If the story is less than one hour, then
the number of minutes is returned.
*/
function CalculateAgeOfStory($storyDate, $hourOffset)
{
// If the story is more than a day old, then just return the story date formatted as m/d/yyyy
$return = date('n/j/Y', mktime($storyDate->Hour, $storyDate->Minute, $storyDate->Second, $storyDate->Month, $storyDate->Day, $storyDate->Year));
// Get the current DateTime
$currentDateArray = getdate();
// Calculate the timestamp for the current DateTime incremented with the hour offset
$currentTime = mktime($currentDateArray['hours'] + $hourOffset, $currentDateArray['minutes'], $currentDateArray['seconds'], $currentDateArray['mon'], $currentDateArray['mday'], $currentDateArray['year']);
// Calculate teh timestamp for the raw story date
$mediaTime = mktime($storyDate->Hour, $storyDate->Minute, $storyDate->Second, $storyDate->Month, $storyDate->Day, $storyDate->Year);
// Calculate the difference between the two timestamps
$timeOffset = $currentTime - $mediaTime;
// Divide the difference between the two timestamps by the number of seconds in a day.
$days = $timeOffset / 86400;
// If the absolute value is less than one day then calculate the number of hours or minutes to return.
if (abs($days) < 1)
{
// Get the remainder between the time remaining and the number of seconds in a day
// then divide by the number of seconds in an hour. This is the number of hours old
// the story is.
$hours = ($timeOffset % 86400) / 3600;
// If the absolute value is less than one hour, then calculate the number of minutes
if (abs($hours) < 1)
{
// Get the remainder between the hours and divide by the number of seconds in a minute
$minutes = ($hours % 3600) / 60;
// Return the number of minutes.
$return = abs(floor($minutes)).' minutes ago.';
}
else
{
// Return the number of hours.
$return = abs(floor($hours)).' hours ago.';
}
}
return $return;
}
/*
Handle the user request for query word marking. Query words are marked with
the Unicode characters 0xE000 and 0xE001 (begin and end, respectively).
*/
function HighlightKeywords($text)
{
$returnValue = $text; /* Default the return value to the parameter input in case highlighting is not enabled. */
$markBegin = '?'; /* Unicode Character 0xE000 */
$markEnd = '?'; /* Unicode Character 0xE001 */
// check that the Mark Query Words option is enabled.
if (empty($markQueryWords))
{
// replace all instances of the opening mark.
$workingValue = str_replace($markBegin, '<b>', $text);
// replace all instances of the closing mark.
$returnValue = str_replace($markEnd, '</b>', $workingValue);
}
return $returnValue;
}
/*
This routine handles formatting any spelling suggestions if any were found.
*/
function HandleSpellingSuggestions($sourceResponse)
{
// Get the spelling suggestion from the Spelling SourceResponse.
$spellingSuggestion = $sourceResponse[1]->Results->Result->Title;
// question if they were looking for the suggested spelling with a link that will reset the search word and resubmit the form.
print '<h3>Were you looking for <a href="Javascript:window.parent.SetQueryString(\''.$spellingSuggestion.'\');window.parent.SubmitQueryString();">'.$spellingSuggestion.'</a>?</h3><br>';
// print out any responses that may have been returned.
PrintResponses($sourceResponse);
}
?>