Sample Code
php
Sample:
// URL parameter from Notification API. (or copy from your project list page.) $projectID=$_GET['projectId']; // Authorization $auth="YOURAPIKEY"; // Header $options = array( 'http' => array( 'method' => 'GET', 'header'=> "Content-Type: application/json\r\n"."Accept: application/json\r\n"."Authorization: Basic ".base64_encode($auth)."\r\n" ) ); $url = 'https://cloud.measuresquare.com/api/projects/'.$projectID; $context = stream_context_create( $options ); $result = file_get_contents( $url, false, $context ); // it is more better to check the HTTP status code here $response = json_decode( $result ); // do your other work next
C#
Sample:
string url = "https://cloud.measuresquare.com/api/projects/" + ProjectIdParameter; using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); string parameter = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}", YOURAPIKEY))); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", parameter); using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url)) { using (HttpResponseMessage response = await client.SendAsync(request)) { if (response.StatusCode != HttpStatusCode.OK) return default(T); // read the data and convert to your data type T result = await response.Content.ReadAsAsync(); // return data, or do what you want to do } } }
CURL
Sample:
BOOL B2BWebServices::ExecuteM2URLGETSSL(CString url, CString apiKey) { char *versionStr = curl_version(); struct curl_slist *list = NULL; CURL* curl = curl_easy_init(); if(curl) { sprintf(gBuf, "%s", url); // Tell libcurl the URL curl_easy_setopt(curl,CURLOPT_URL, gBuf); // Tell libcurl what function to call when it has data curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,Handle_dataXML); // Setup Header for M2 call list = curl_slist_append(list, "Content-Type: application/xml"); list = curl_slist_append(list, "Accept: application/xml"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list); // User Name is API Key CURLcode retVal = curl_easy_setopt(curl, CURLOPT_USERNAME, apiKey); // Verify Host CURLcode retVal2 = curl_easy_setopt(curl,CURLOPT_SSL_VERIFYHOST,0L); // Do it! CURLcode res = curl_easy_perform(curl); //get the http codes long http_code = 0; curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); // if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK) curl_easy_cleanup(curl); } else return FALSE; return TRUE; }