smaction

KeywordClass

Mar 28th, 2019
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.16 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: scott
  5.  * Date: 3/4/19
  6.  * Time: 3:42 PM
  7.  */
  8.  
  9. class KeywordClass
  10. {
  11.  
  12.     public function accessEspo($url,$action,$user,$password){
  13.  
  14.         $credentials = $user.':'.$password;
  15.         $curl = curl_init();
  16.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  17.         curl_setopt($curl, CURLOPT_USERPWD, $credentials);
  18.         curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  19.         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  20.         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  21.         curl_setopt($curl, CURLOPT_URL, $url . $action);
  22.         curl_setopt($curl, CURLOPT_HEADER, false);
  23.  
  24.         $result = curl_exec($curl);
  25.         $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  26.         curl_close($curl);
  27.  
  28.         if($httpCode < 400){
  29.  
  30.             $json = json_decode($result, TRUE);
  31.         }
  32.         else{
  33.             echo $url.$action;
  34.             echo 'something went wrong!<BR>';
  35.             echo $httpCode;
  36.         }
  37.         return($json);
  38.     }
  39.  
  40.  
  41.  
  42.     public function getNumResults($searchquery) {
  43.         $googleAPIKey = "xxxxxxx"; //Google API Key for JSON
  44.         $googleSearchEngineID = "xxxxxxxxx"; //Google Custom Search engine ID
  45.         $encodedquery = urlencode($searchquery);
  46.  
  47.         $URL = "https://www.googleapis.com/customsearch/v1?";
  48.         $action = "key=".$googleAPIKey."&cx=".$googleSearchEngineID."&q=".$encodedquery;
  49.  
  50.         $ch = curl_init();
  51.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  52.         curl_setopt($ch, CURLOPT_URL, $URL . $action);
  53.  
  54.         $result = curl_exec($ch);
  55.         $response = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
  56.         curl_close($ch);
  57.  
  58.         if ($response == 200) {
  59.             $decodedjson = json_decode($result, true);
  60.             $numberresults = $decodedjson['queries']['request'][0]['totalResults'];
  61.             return $numberresults;
  62.         }
  63.  
  64.         else {return "HTTP Response Error: ".curl_getinfo($ch, CURLINFO_RESPONSE_CODE);}
  65.     }
  66.  
  67.     public function editEspo($url,$action,$params,$user,$password){
  68.  
  69.         $credentials = $user.':'.$password;
  70.  
  71.         $curl = curl_init();
  72.         //curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  73.         curl_setopt($curl, CURLOPT_USERPWD, $credentials);
  74.         curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  75.         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  76.         //curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  77.         curl_setopt($curl, CURLOPT_URL, $url . $action);
  78.         curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  79.         curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
  80.  
  81.  
  82.         curl_setopt($curl, CURLOPT_POST, true);
  83.  
  84.         $result = curl_exec($curl);
  85.         $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  86.         curl_close($curl);
  87.  
  88.         if($httpCode < 400){
  89.  
  90.             $json = json_decode($result, TRUE);
  91.         }
  92.         else{
  93.  
  94.  
  95.             echo $url.$action;
  96.             echo '<BR>';
  97.             echo 'something went wrong!<BR>';
  98.             echo $httpCode;
  99.             echo '<BR>';
  100.  
  101.         }
  102.         return($json);
  103.     }
  104.  
  105.  
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment