| 1 |
<?php |
| 2 |
if( ! defined( 'ABSPATH') ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* UseTopic API's |
| 8 |
*/ |
| 9 |
|
| 10 |
class UseTopicAPI { |
| 11 |
|
| 12 |
// url to usetopic api |
| 13 |
protected $apiUrl = 'https://app.usetopic.com/api/'; |
| 14 |
protected $username; |
| 15 |
protected $password; |
| 16 |
protected $token; |
| 17 |
protected $briefID; |
| 18 |
protected $briefcontent; |
| 19 |
protected $searchkey; |
| 20 |
|
| 21 |
public function authenticateUsetopicUser( $username, $password ){ |
| 22 |
|
| 23 |
$this->username = $username; |
| 24 |
$this->password = $password; |
| 25 |
|
| 26 |
$url = $this->apiUrl . 'login?email='.urlencode( $this->username ).'&password='.urlencode( $this->password ); |
| 27 |
return $response = $this->useTopicAuthCurl( $url ); |
| 28 |
|
| 29 |
|
| 30 |
} |
| 31 |
|
| 32 |
public function getBriefsbyTopicToken( $token ){ |
| 33 |
|
| 34 |
$this->token = $token; |
| 35 |
return $response = $this->useTopicBriefsCurl( $this->token ); |
| 36 |
|
| 37 |
} |
| 38 |
|
| 39 |
public function getBriefDetailByID( $briefid, $token ){ |
| 40 |
$this->token = $token; |
| 41 |
$this->briefID = $briefid; |
| 42 |
return $response = $this->useTopicBriefDetailCurl( $this->briefID, $this->token ); |
| 43 |
|
| 44 |
} |
| 45 |
public function getBriefDetailByURL( $briefid ){ |
| 46 |
$this->briefID = $briefid; |
| 47 |
return $response = $this->useTopicBriefDetailURLCurl( $this->briefID ); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
public function getBriefUpdatedReport( $briefid, $token, $content ){ |
| 52 |
$this->token = $token; |
| 53 |
$this->briefID = $briefid; |
| 54 |
$this->briefcontent = $content; |
| 55 |
return $response = $this->getBriefUpdatedReportCurl( $this->briefID, $this->token, $this->briefcontent ); |
| 56 |
|
| 57 |
} |
| 58 |
public function getBriefUpdatedReportURL( $briefid, $content ){ |
| 59 |
|
| 60 |
$this->briefID = $briefid; |
| 61 |
$this->briefcontent = $content; |
| 62 |
return $response = $this->getBriefUpdatedReportURLCurl( $this->briefID, $this->briefcontent ); |
| 63 |
|
| 64 |
} |
| 65 |
public function getBriefsBySearch( $search, $briefToken ){ |
| 66 |
|
| 67 |
$this->searchkey = $search; |
| 68 |
$this->token = $briefToken; |
| 69 |
return $response = $this->getBriefsBySearchCurl( $this->searchkey, $this->token ); |
| 70 |
|
| 71 |
} |
| 72 |
private function useTopicAuthCurl( $url ){ |
| 73 |
$curl = curl_init(); |
| 74 |
curl_setopt_array($curl, array( |
| 75 |
CURLOPT_URL => $url, |
| 76 |
CURLOPT_RETURNTRANSFER => true, |
| 77 |
CURLOPT_ENCODING => '', |
| 78 |
CURLOPT_MAXREDIRS => 10, |
| 79 |
CURLOPT_TIMEOUT => 0, |
| 80 |
CURLOPT_FOLLOWLOCATION => true, |
| 81 |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
| 82 |
CURLOPT_CUSTOMREQUEST => 'GET', |
| 83 |
)); |
| 84 |
|
| 85 |
$response = curl_exec( $curl ); |
| 86 |
|
| 87 |
curl_close( $curl ); |
| 88 |
return $response; |
| 89 |
} |
| 90 |
|
| 91 |
private function useTopicBriefsCurl( $token ){ |
| 92 |
$curl = curl_init(); |
| 93 |
|
| 94 |
curl_setopt_array($curl, array( |
| 95 |
CURLOPT_URL => $this->apiUrl.'keyword_reports/', |
| 96 |
CURLOPT_RETURNTRANSFER => true, |
| 97 |
CURLOPT_ENCODING => '', |
| 98 |
CURLOPT_MAXREDIRS => 10, |
| 99 |
CURLOPT_TIMEOUT => 0, |
| 100 |
CURLOPT_FOLLOWLOCATION => true, |
| 101 |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
| 102 |
CURLOPT_CUSTOMREQUEST => 'GET', |
| 103 |
CURLOPT_HTTPHEADER => array( |
| 104 |
'Authorization: Bearer '.$token |
| 105 |
), |
| 106 |
)); |
| 107 |
|
| 108 |
$response = curl_exec($curl); |
| 109 |
|
| 110 |
curl_close($curl); |
| 111 |
return $response; |
| 112 |
} |
| 113 |
private function useTopicBriefDetailCurl( $briefid, $token ){ |
| 114 |
|
| 115 |
$curl = curl_init(); |
| 116 |
|
| 117 |
curl_setopt_array($curl, array( |
| 118 |
CURLOPT_URL => $this->apiUrl.'keyword_reports/'.$briefid, |
| 119 |
CURLOPT_RETURNTRANSFER => true, |
| 120 |
CURLOPT_ENCODING => '', |
| 121 |
CURLOPT_MAXREDIRS => 10, |
| 122 |
CURLOPT_TIMEOUT => 0, |
| 123 |
CURLOPT_FOLLOWLOCATION => true, |
| 124 |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
| 125 |
CURLOPT_CUSTOMREQUEST => 'GET', |
| 126 |
CURLOPT_HTTPHEADER => array( |
| 127 |
'Authorization: Bearer '.$token |
| 128 |
), |
| 129 |
)); |
| 130 |
|
| 131 |
$response = curl_exec($curl); |
| 132 |
|
| 133 |
curl_close($curl); |
| 134 |
return $response; |
| 135 |
} |
| 136 |
private function useTopicBriefDetailURLCurl( $briefid ){ |
| 137 |
|
| 138 |
$curl = curl_init(); |
| 139 |
|
| 140 |
curl_setopt_array($curl, array( |
| 141 |
CURLOPT_URL => $this->apiUrl.'keyword_reports/'.$briefid, |
| 142 |
CURLOPT_RETURNTRANSFER => true, |
| 143 |
CURLOPT_ENCODING => '', |
| 144 |
CURLOPT_MAXREDIRS => 10, |
| 145 |
CURLOPT_TIMEOUT => 0, |
| 146 |
CURLOPT_FOLLOWLOCATION => true, |
| 147 |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
| 148 |
CURLOPT_CUSTOMREQUEST => 'GET', |
| 149 |
CURLOPT_HTTPHEADER => array(), |
| 150 |
)); |
| 151 |
|
| 152 |
$response = curl_exec($curl); |
| 153 |
|
| 154 |
curl_close($curl); |
| 155 |
return $response; |
| 156 |
} |
| 157 |
|
| 158 |
private function getBriefUpdatedReportCurl( $briefid, $token, $content){ |
| 159 |
//return json_encode(array($briefid, $token, $content)); |
| 160 |
$curl = curl_init(); |
| 161 |
|
| 162 |
curl_setopt_array($curl, array( |
| 163 |
CURLOPT_URL => $this->apiUrl.'keyword_reports/'.$briefid, |
| 164 |
CURLOPT_RETURNTRANSFER => true, |
| 165 |
CURLOPT_ENCODING => '', |
| 166 |
CURLOPT_MAXREDIRS => 10, |
| 167 |
CURLOPT_TIMEOUT => 0, |
| 168 |
CURLOPT_FOLLOWLOCATION => true, |
| 169 |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
| 170 |
CURLOPT_CUSTOMREQUEST => 'PUT', |
| 171 |
CURLOPT_POSTFIELDS => '{"editor_content_gdocs":"'.$content.'"}', |
| 172 |
CURLOPT_HTTPHEADER => array( |
| 173 |
'Authorization: Bearer '.$token, |
| 174 |
'Content-Type: application/json' |
| 175 |
), |
| 176 |
)); |
| 177 |
|
| 178 |
$response = curl_exec($curl); |
| 179 |
|
| 180 |
curl_close($curl); |
| 181 |
return $response; |
| 182 |
} |
| 183 |
|
| 184 |
private function getBriefUpdatedReportURLCurl( $briefid, $content){ |
| 185 |
//return json_encode(array($briefid, $token, $content)); |
| 186 |
$curl = curl_init(); |
| 187 |
|
| 188 |
curl_setopt_array($curl, array( |
| 189 |
CURLOPT_URL => $this->apiUrl.'keyword_reports/'.$briefid, |
| 190 |
CURLOPT_RETURNTRANSFER => true, |
| 191 |
CURLOPT_ENCODING => '', |
| 192 |
CURLOPT_MAXREDIRS => 10, |
| 193 |
CURLOPT_TIMEOUT => 0, |
| 194 |
CURLOPT_FOLLOWLOCATION => true, |
| 195 |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
| 196 |
CURLOPT_CUSTOMREQUEST => 'PUT', |
| 197 |
CURLOPT_POSTFIELDS => '{"editor_content_gdocs":"'.$content.'"}', |
| 198 |
CURLOPT_HTTPHEADER => array('Content-Type: application/json'), |
| 199 |
)); |
| 200 |
|
| 201 |
$response = curl_exec($curl); |
| 202 |
|
| 203 |
curl_close($curl); |
| 204 |
return $response; |
| 205 |
|
| 206 |
|
| 207 |
} |
| 208 |
private function getBriefsBySearchCurl( $search, $token ){ |
| 209 |
if(empty($search)){ |
| 210 |
$utAPIurl = $this->apiUrl.'keyword_reports/'; |
| 211 |
}else{ |
| 212 |
$utAPIurl = $this->apiUrl.'keyword_reports?filters=filter%5BBuser_id%5DD=all&filter%5Bsearch%5D='.$search; |
| 213 |
} |
| 214 |
$curl = curl_init(); |
| 215 |
|
| 216 |
curl_setopt_array($curl, array( |
| 217 |
CURLOPT_URL => $utAPIurl, |
| 218 |
CURLOPT_RETURNTRANSFER => true, |
| 219 |
CURLOPT_ENCODING => '', |
| 220 |
CURLOPT_MAXREDIRS => 10, |
| 221 |
CURLOPT_TIMEOUT => 0, |
| 222 |
CURLOPT_FOLLOWLOCATION => true, |
| 223 |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
| 224 |
CURLOPT_CUSTOMREQUEST => 'GET', |
| 225 |
CURLOPT_HTTPHEADER => array('Authorization: Bearer '.$token, |
| 226 |
), |
| 227 |
)); |
| 228 |
|
| 229 |
$response = curl_exec($curl); |
| 230 |
|
| 231 |
curl_close($curl); |
| 232 |
return $response; |
| 233 |
} |
| 234 |
|
| 235 |
} |