I'm trying to connect with hubstaff api, has anyone ever tried it? I'm a newbie in php-cURL, how do you convert this to PHP Curl?
curl -H "App-Token: BMyQnju-4tknuBQMsN0ujr6NWF5ohQaP9de8AWMJXik" -H "Auth-Token: X-vfv2c7jf_0NKoHLbX1t4yftK-TI-jZ4d7roNegw24" "http://api.hubstaff.com/v1/users"
It also would not show any result of I do this:
// Standard data
$data['app_token'] = $this->app_token;
// Debugging output
$this->debug = array();
$this->debug['HTTP Method'] = $http_method;
// Create a cURL handle
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'App-Token: ' . $this->app_token,
'Content-Type: application/xml'
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// Send data
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// Debugging output
$this->debug['Posted Data'] = $data;
}
// Execute cURL request
$curl_response = curl_exec($ch);
// Save CURL debugging info
$this->debug['Last Response'] = $curl_response;
$this->debug['Curl Info'] = curl_getinfo($ch);
// Close cURL handle
curl_close($ch);
// Parse response
$response =$curl_response;// $this->parseAsciiResponse($curl_response);
// Return parsed response
return $response;
Im just trying to get my Auth-Token
Any help would be greatly appreciated.
#Michal I have solved my own problem and created this simple class to help anyone else in connecting with hubstaff fast. feel free for any suggestions and optimizations
class HubstaffApi {
private $app_token = '';
private $auth_token = '';
private $debug = [];
public function __construct($app_token, $auth_token) {
$this->app_token = $app_token;
$this->auth_token = $auth_token;
}
private function sendRequest($api_method, $http_method = 'GET', $data = null) {
// Standard data
$data['app_token'] = $this->app_token;
$request_url = "https://api.hubstaff.com/v1/";
// Debugging output
$this->debug = array();
$this->debug['Request URL'] = $request_url . $api_method;
// Create a cURL handle
$ch = curl_init();
// Set the request
curl_setopt($ch, CURLOPT_URL, $request_url . $api_method);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'App-Token: ' . $this->app_token,
'Auth-Token: ' . $this->auth_token
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $http_method);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// Send data
if (!empty($data)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
// Debugging output
$this->debug['Posted Data'] = $data;
}
// Execute cURL request
$curl_response = curl_exec($ch);
// Save CURL debugging info
$this->debug['Last Response'] = $curl_response;
$this->debug['Curl Info'] = curl_getinfo($ch);
// Close cURL handle
curl_close($ch);
// Parse response
$response = $curl_response;
// Return parsed response
return $response;
}
public function users(array $parameters = array()) {
return $this->sendRequest('users', 'GET', $parameters);
}
public function activities(array $parameters = array()) {
return $this->sendRequest('activities', 'GET', $parameters);
}
public function screenshots(array $parameters = array()) {
return $this->sendRequest('screenshots', 'GET', $parameters);
}
}
You can simply use this by:
$Hubstaff = new HubstaffApi(
YOUR_APP_TOKEN,
YOUR_AUTH_TOKEN); //simply get auth token in developer.hubstaff 's generator, it doesn't expire anyway.
$response = $Hubstaff->activities([
"start_time" => "2015-09-10T00:00:00+08:00:00",
"stop_time" => "2015-09-10T24:00:00+08:00:00",
"users" => YOUR_HUBSTAFF_ID
]);
echo $response;
Related
I'm trying to make a post request with php using curl however the json is not getting delivered to the REST API. Here is my code. In the webservice all I get is null value. I'm not sure where I'm going wrong.
$email_json_data = json_encode($email_data);
$header[] = "Content-type: application/json";
$ch = curl_init($api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $email_json_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
return $response;
Webservice code:
$email_json_data = $this->post('email_json_data');
$email_data = json_decode($email_json_data);
Check PHP: curl_errno
There's probably a problem connecting to the server, and it's probably in one of your $header. To find out more, you need to show (in production, LOG it) the curl error.
In the future, please try to include a complete code sample, rather than just snippets
Code added from PHP: curl_strerror
class CurlAdapter
{
private $api_url = 'www.somewhere.com/api/server.php';
private $error = "";
private function jsonPost($data)
{
// init curl
$ch = curl_init($this->api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl header
$header[] = "Content-type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// build post data
$post_data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// execute
if (empty($response = curl_exec($ch)) {
// Check for errors and display the error message
if($errno = curl_errno($ch)) {
$error_message = curl_strerror($errno);
$this->error = "cURL error ({$errno}):\n {$error_message}";
// #todo log curl error
}
}
// Close the handle
curl_close($ch);
return $response;
}
public function post( mixed $data )
{
if (empty($this->jsonPost($data))) {
return $this->error;
}
return $response;
}
}
$ca = new CurlAdapter();
echo $ca->post(['data' => 'testdata']);
Figured out a way to make this work.
Replaced $email_json_data = $this->post('email_json_data');
with $email_json_data = file_get_contents("php://input");
Is there a way to programmatically login to Airbnb with email/password through a CLI PHP script? and get a response back?
Thanks.
If you're looking to remotely log in to Airbnb and return information, you can use cURL to post data to Airbnb and return the results.
Examples on how to post form data can be found all over the web, however, a very reputable tutorial can be found here. Essentially, you want to cURL the login page, and include the login information with POST.
<?php
// A very simple PHP example that sends a HTTP POST to a remote site
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.airbnb.com/login");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"email=" . $email . "&password=" . $password);
// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('email' => $email, 'password' => $password)));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") { ... } else { ... }
?>
Make sure you check out this answer on SO, as well as the tutorials here.
Yes, there is. Airbnb's API isn't open to the general public, but if you sniff the traffic from your phone, you can see what requests are being sent to which endpoints. I experimented a little bit with their API and it follows as such for logging in
<?php
class AirBnB {
// Define the properties
public $currency = 'USD';
public $language = 'en';
public $country = 'us';
public $network = 'AT&T';
public $apiKey = '915pw2pnf4h1aiguhph5gc5b2';
public $adId = '911EBF1C-7C1D-46D5-A925-2F49ED064C92';
public $deviceId = 'a382581f36f1635a78f3d688bf0f99d85ec7e21f';
public function SendRequest($endpoint, $token, $post, $data, $cookies) {
$headers = array(
'Host: api.airbnb.com',
'Accept: application/json',
'Accept-Language: en-us',
'Connection: keep-alive',
'Content-Type: application/json',
'Proxy-Connection: keep-alive',
'X-Airbnb-Carrier-Country: '.$this->country,
'X-Airbnb-Currency: '.$this->currency,
'X-Airbnb-Locale: '.$this->language,
'X-Airbnb-Carrier-Name: '.$this->network,
'X-Airbnb-Network-Type: wifi',
'X-Airbnb-API-Key: '.$this->apiKey,
'X-Airbnb-Device-ID: '.$this->deviceId,
'X-Airbnb-Advertising-ID: '.$this->adId,
);
// Add the new custom headers
if($token) {
$header = 'X-Airbnb-OAuth-Token: '.$token;
array_push($headers, $header);
}
// Add the query string
if(!$post && is_array($data)) {
$endpoint .= '?'.http_build_query($data);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.airbnb.com/'.$endpoint);
curl_setopt($ch, CURLOPT_USERAGENT, 'Airbnb/15.50 iPhone/9.2 Type/Phone');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if($post) {
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
}
if($cookies) {
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
} else {
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
}
$response = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array(
'http' => $http,
'response' => $response
);
}
public function Authorize($username, $password) {
$data = array(
'username' => $username,
'password' => $password,
'prevent_account_creation' => TRUE,
);
$data = $this->SendRequest('v1/authorize', FALSE, TRUE, $data, FALSE);
if($data['http'] == 200) {
$json = #json_decode($data['response'], TRUE);
return $json['access_token'];
} else {
return FALSE;
}
}
}
// Call a new instance of AirBnB
$airbnb = new AirBnB;
// Get the OAuth token
$token = $airbnb->Authorize('my#email.com', 'password');
?>
You can find out more about their API here.
I am new to working with api and have been searching for the little tiny piece of code that makes the following snippit possible, but I am having trouble finding it. I have been watching tutorials on jquery, ajax, json, and php but have not found exactly what I need. If someone could show me a simple example of how the request would be constructed from the opening tag of the file to the closing tag it would be very helpful. After I get the data returned I can figure out how to parse and style it for display.
The first piece of code is the example that someone else said they use but I tried it (with my own api key) and I seem to be missing something. The second code is the "Mmjmenu.php" file that I assume needs to be in the same directory as the first php file. Following the code is the error I get.
("my api key" is replacing my actual key)
<?php
require 'API/Mmjmenu.php';
$client = new Mmjmenu('my api key');
$menuItems = $client->menuItems();
$menuItems = json_decode($menuItems, true);
foreach($menuItems['menu_items'] as $item)
{
echo $item['name'];
}
?>
This is the "Mmjmenu.php" file (can be found on git hub):
<?php
class Mmjmenu {
private $domain = 'https://mmjmenu.com/api/v1';
private $active_api_key;
private $active_domain;
private $username;
private $password;
public function __construct($api_key, $active_domain = null, $active_api_key = null) {
$this->setActiveDomain($this->domain, $api_key);
}
public function setActiveDomain($active_domain, $active_api_key) {
$this->active_domain = $active_domain;
$this->active_api_key = $active_api_key;
$this->username = $this->active_api_key;
$this->password = 'x';
}
private function sendRequest($uri, $method = 'GET', $data = '') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://mmjmenu.com/api/v1" . $uri);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
$method = strtoupper($method);
if($method == 'POST')
{
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else if ($method == 'PUT')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else if($method != 'GET')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = new StdClass();
$result->response = curl_exec($ch);
$result->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result->meta = curl_getinfo($ch);
$curl_error = ($result->code > 0 ? null : curl_error($ch) . ' (' . curl_errno($ch) . ')');
curl_close($ch);
if ($curl_error) {
//print('ERROR');
}
return $result;
}
/****************************************************
********************* MENU ITEMS ********************
****************************************************/
public function menuItems() {
$base_url = '/menu_items';
$menuItems = $this->sendRequest($base_url);
return $menuItems->response;
}
public function menuItem($id) {
$base_url = "/menu_items/$id";
$menuItem = $this->sendRequest($base_url);
return $menuItem->response;
}
}
?>
This is the error I get when executing the first file:
Fatal error: Class 'Mmjmenu' not found in /home/...(my directory).../mmtest.php on line 4
I am trying to use getSearchRecordsByPDC method which can be found here https://www.zoho.com/crm/help/api/getsearchrecordsbypdc.html#Request_URL
I have this code:
private $token = '1234567890abcdefg';
public $responseType = 'xml';
public function getSearchRecordsByPDC($searchValue,$searchColumn='email')
{
$url = "https://crm.zoho.com/crm/private/".$this->responseType."/Leads/getSearchRecordsByPDC?newFormat=1&authtoken=".$this->token."&scope=crmapi&selectColumns=Leads(First Name,Lead Source,Phone,Mobile,Website,Lead Status,Description,Last Name,Website,Email,Lead Owner)&searchColumn=$searchColumn&searchValue=$searchValue";
$result = $this->curlRequest($url);
return $result;
}
public function curlRequest($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$data = $zoho->getSearchRecordsByPDC('my_email#gmail.com');
print_r($data);
I just posted some snippets of my code to not appear it to be very long.
When running this code. I am not getting any response, even an error message or whatsoever, like I am getting a blank response, no xml response or whatever. But when ever I try to copy and paste the $url variable output into my web browser, I am getting response, and those response are valid.
What's wrong with this? Your help will be greatly appreciated! Thanks!
It looks like you are mixing up OOP and procedural code. Try this:
class Zoho {
private $token = '1234567890abcdefg';
public $responseType = 'xml';
public function getSearchRecordsByPDC($searchValue,$searchColumn='email')
{
$url = "https://crm.zoho.com/crm/private/".$this->responseType."/Leads/getSearchRecordsByPDC?newFormat=1&authtoken=".$this->token."&scope=crmapi&selectColumns=Leads(First Name,Lead Source,Phone,Mobile,Website,Lead Status,Description,Last Name,Website,Email,Lead Owner)&searchColumn=$searchColumn&searchValue=$searchValue";
$result = $this->curlRequest($url);
return $result;
}
public function curlRequest($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}
$zoho = new Zoho;
$data = $zoho->getSearchRecordsByPDC('my_email#gmail.com');
print_r($data);
having a bit of an issue with the twitter API. When I send something to https://api.twitter.com/1/statuses/update.json, the tweet (status update) does get sent, however, I do not get a response from twitter. When I send requests to any of the other api urls they work as expected and do return a response. Please see code below...
function postStatus($oauthToken,$status) {
//Create sig base string
$tokenddata = array('oauth_token'=>$oauthToken['oauth_token'],'oauth_token_secret'=>$oauthToken['oauth_token_secret']);
$status = rawurlencode($status);
$baseurl = $this->baseurl . "statuses/update.json";
$url = "{$baseurl}?status={$status}";
$authHeader = get_auth_header($url, $this->_consumer['key'], $this->_consumer['secret'],
$tokenddata, 'POST', $this->_consumer['algorithm']);
$postfields = "status={$status}";
$response = $this->_connect($url,$authHeader,$postfields,'POST');
return json_decode($response);
}
private function _connect($url, $auth, $postfields=null, $method='GET') {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($ch, CURLOPT_SSLVERSION,3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array($auth));
if ($method == 'POST') {
curl_setopt($ch, CURLOPT_POST, TRUE);
if (!empty($postfields)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
}
}
$curl_info = curl_getinfo($ch);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
And as I said before, the other requests that I am using are 'GET' requests and use the code below...
function getFromTwitter($url, $oauthToken, $params=null) {
$tokenddata = array('oauth_token'=>$oauthToken['oauth_token'],'oauth_token_secret'=>$oauthToken['oauth_token_secret']);
$baseurl = $this->baseurl . $url;
if(!empty($params)) {
$fullurl = $baseurl . "?" . build_http_query($params);
$postfields = build_http_query($params);
$authHeader = get_auth_header($fullurl, $this->_consumer['key'], $this->_consumer['secret'],
$tokenddata, 'GET', $this->_consumer['algorithm']);
} else {
$authHeader = get_auth_header($baseurl, $this->_consumer['key'], $this->_consumer['secret'],
$tokenddata, 'GET', $this->_consumer['algorithm']);
}
if(!empty($postfields)) {
$response = $this->_connect($fullurl,$authHeader);
} else {
$response = $this->_connect($baseurl,$authHeader);
}
return json_decode($response);
}
Thanks for all of the help!
-SM
Implementing code for social networks on your own can be a pain (in my opinion)
It would be easier for you to use twitter-async (https://github.com/jmathai/twitter-async)
I have added it before to my CI as a helper function then used it as is.
It was easy to use & well documented.