How to Set Up a New Qhub in Just 3 Minutes

Ganeshmr

Can you please provide an example of using api using curl. This will help in understanding the api use for other languages like js i am struggling to post question using command line. Following is what i tried. Curl -X POST -d "question='this is sample question'&tags='test'&user_id='11111'" --user 11111:***** api.qhub.com/example.qhub.com/ask.xml now i dont know where i need to pass the apikey. Please help?

Asked by Ganeshmr 6 years ago help api


Muhammad Omar Khan
0
 
Hi, I am not sure if I can provide you an example specific to JS. But here is a sample code for the PHP which should help you proceed in the right direction.

$apiurl = "api.qhub.com";$hub = "example.qhub.com";           // This should be the URL of your HUB$apikey = "Q_hg3reeb20e80257ed0934530f4b513a6";    //This should be your API Key$user_id = "";  //Enter your user id here$password  = ""; //Enter your password here

$url = $apiurl."/".$hub."/ask.json";

$postfields = array(  'question'=>"Where Can I Get Help With Qhub?",  'tags'=>'qhub,help',  'user_id'=>'your_user_id' );                                                                              //AUTHENTICATION PARAMETERES $username=($user_id && $password) ? Md5($user_id.":".$password) : "X"; $ret = "$username:".$apikey;

//CALL THE API         $ch = curl_init();        curl_setopt ($ch, CURLOPT_URL, $url);        curl_setopt ($ch, CURLOPT_REFERER, $url);        curl_setopt ($ch, CURLOPT_USERAGENT, "Qhub PHP API v1.0");

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);        curl_setopt($ch, CURLINFO_HEADER_OUT, true);        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);        curl_setopt ($ch, CURLOPT_TIMEOUT, 10);                curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);         curl_setopt($ch, CURLOPT_USERPWD, $ret);                 if ($postfields) {            curl_setopt ($ch, CURLOPT_POST, 1);            curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields);

                $response = curl_exec ($ch);        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        if (curl_errno($ch)) {  print "error";        }         else {  curl_close($ch);        }                if($httpcode==401){  ob_end_clean();  header('HTTP/1.1 401 Unauthorized');  echo $response;  echo "api password: "."";  echo "apikey: "."";  echo "Authorization Required.";  exit;        }        else {   $arr_response = json_decode($response, true);     if(!is_array($arr_response))   {     print "error receiving a response";                       exit();   }    else if($arr_response['errors'])   {        // get the first error     foreach($arr_response['errors'] as $arr_errors)    {       $error = is_array($arr_errors)?$arr_errors[0]:$arr_errors;        break;       }                                                                    print "error";    exit;  }   else    {                    print $arr_response;   }                                }

     ?>

by Muhammad Omar K...  6 years ago

Answer this question

Can you please provide an example of using api using curl. This will help in understanding the api use for other languages like js i am struggling to post question using command line. Following is what i tried. Curl -X POST -d "question='this is sample question'&tags='test'&user_id='11111'" --user 11111:***** api.qhub.com/example.qhub.com/ask.xml now i dont know where i need to pass the apikey. Please help?

0 errors found:

 
0
 
 

Help

Qhub Help
Please feel free to leave us your question.