Recently, I'm developing my site with social networking, and want to import gmail,yahoo,hotmail etc.. contacts.
I have already grabbed contacts from Gmail and now next want to do the same with Yahoo. Reading the Yahoo documentation and following my gmail code, I get the authorisation screen and get the auth code back, but unable to get the access token. (I am using curl and I am expecting help related to it).
Here is the dump: string
'code=hzbp3px&client_id=dj0yJmk9WlJ0a3dmQTQ3ZFcyJmQ9WVdrOVFXRXliSFpMTm1zbWNHbzlNQS0tJnM9Y29uc3VtZXJzZWNyZXQmeD1jZA--&client_secret=5b3a803684538bae27519af3e34f3e268da432a1&redirect_uri=http://localhost/DEVELOPMENT/Grabber.php&grant_type=authorization_code' (length=254)
object(stdClass)[5] public 'error' => string 'invalid_request'
(length=15)
Here is my functions:
public function get_url(){
global $session;
switch ($session->get_provider()) {
case "gmail": return "https://accounts.google.com/o/oauth2/token";
break;
case "yahoo": return "https://api.login.yahoo.com/oauth2/get_token";
break;
}
}
public function curl_query($post,$boolean){
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,$this->get_url());
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
var_dump($response);
//exit;
switch ($boolean) {
case "true": return $response->access_token;
case "false": return $response->refresh_token;
}
}
public function get_access_token($auth_code){
global $session;
switch ($session->get_provider()) {
case "gmail": return $this->curl_query($this->post_params($auth_code,'access_token'),true);
case "yahoo":
$accesstoken = $this->curl_query($this->post_params($auth_code,'access_token'),true);
//$refreshtoken = $this->curl_query($this->post_params($accesstoken),false);
return $accesstoken;
}
}
public function post_params($auth_code,$token){
global $session;
if($token == 'access_token'){
$fields=array(
'code'=> $auth_code,
'client_id'=> $session->get_client_id(),
'client_secret'=> $session->get_client_secret(),
'redirect_uri'=> $session->get_redirect_uri(),
'grant_type'=> 'authorization_code'
);
} else if($token == 'refresh_token'){
$fields=array(
'refresh_token'=> $auth_code,
'client_id'=> $session->get_client_id(),
'client_secret'=> $session->get_client_secret(),
'redirect_uri'=> $session->get_redirect_uri(),
'grant_type'=> 'authorization_code'
);
}
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
var_dump($post);
return $post;
}
I found out how to get the tokens, here is the code if anyone needs it
Anyway oauth2 is not working with yahoo as i experienced, bellow i am using oauth1.0
public function get_gmail_access_token($auth_code){
global $session;
$fields=array(
'code'=> $auth_code,
'client_id'=> $session->get_client_id(),
'client_secret'=> $session->get_client_secret(),
'redirect_uri'=> $session->get_redirect_uri(),
'grant_type'=> 'authorization_code'
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$url = 'https://accounts.google.com/o/oauth2/token';
$result = $this->curl_query_post($url,$post);
$response = json_decode($result);
return $response->access_token;
}
public function get_yahoo_access_token($token,$verifier){
global $session;
$tokens = array();
$fields=array(
'oauth_version'=> '1.0',
'oauth_nonce'=> rand(),
'oauth_consumer_key'=> $session->get_client_id(),
'oauth_signature'=> $session->get_client_secret().'%26'.$session->get_token_secret(),
'oauth_timestamp'=> time()+3600,
'oauth_signature_method'=> 'plaintext',
'oauth_token'=> $token,
'oauth_verifier'=> $verifier,
'oauth_callback'=> $session->get_redirect_uri()
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$url = 'https://api.login.yahoo.com/oauth/v2/get_token';
$result = $this->curl_query_post($url,$post);
$string = parse_str($result);
$tokens['oauth_token'] = $oauth_token;
$tokens['oauth_token_secret'] = $oauth_token_secret;
//$xoauth_yahoo_guid;
return $tokens;
}
Related
I'm not sure where I'm going wrong, but needing to post from my site to my Facebook Page.
~ fbautopost.php
require_once("Facebook/facebook.php");
class FacebookPost
{
var $consumer;
var $token;
var $method;
var $http_status;
var $last_api_call;
var $callback;
var $connection;
var $access_token;
function __construct($data)
{
$config = array();
$config['appId'] = $data['consumer_key'];
$config['secret'] = $data['consumer_secret'];
$pageID = $data['page_ID'];
$this->connection = new Facebook($config);
}
function share($title, $targetUrl, $imgUrl, $description, $access_token)
{
$this->connection->setAccessToken($access_token);
$params["access_token"] = $access_token;
if(!empty($title))
{
$params["message"] = $title;
$params["name"] = $title;
}
if(!empty($targetUrl))
{
$params["link"] = $targetUrl;
}
if(!empty($imgUrl))
{
$params["picture"] = $imgUrl;
}
if(!empty($description))
{
$params["description"] = $description;
}
// post to Facebook
try
{
$ret = $this->connection->api('/' .$pageID . '/feed/', 'post', $params);
}
catch(Exception $e)
{
$e->getMessage();
}
return true;
}
function getLoginUrl($params)
{
return $this->connection->getLoginUrl($params);
}
function getContent($url)
{
$ci = curl_init();
/* Curl settings */
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ci, CURLOPT_HEADER, false);
curl_setopt( $ci, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ci);
curl_close ($ci);
return $response;
}
}
~ post.php
include('fbautopost.php');
$access_token = 'MY_ACCESS_TOKEN';
$facebookData = array();
$facebookData['consumer_key'] = 'MY_APP_ID';
$facebookData['consumer_secret'] = 'MY_SECRET_KEY';
$facebookData['page_ID'] = 'MY_PAGE_ID';
$title = 'Post Title';
$description = 'This is a test post';
$facebook = new FacebookPost($facebookData);
$facebook->share($title, $description, $access_token);
I haven't taken the app out of development, but as the admin I am not seeing any test posts posting on the page.
It's possible that I'm not getting the correct ACCESS TOKEN?? From the Graph Explorer, I am selecting my application, then getting a User Token with manage_page and publish_page permissions. I've also tried using a Page Token. Neither work. Is my code bad, or am I missing something else?
TIA
The issue was that I needed to require_once('Facebook/autoload.php'), not the Facebook.php that I had.
My web app stops when it tries executing a function from another file. I am using a include_once to include the file where the function is. I am also pretty sure that the path is correct. (if there is anyway to check let me know). the functions which I am trying to use are written in the class.connections.php . Also as you can see I am echoing 2 test. Only the first one shows.
Invite.php
global $page_array ;
global $current_user ;
include_once(DITC_PATH."/class.friends.php");
include_once(DITC_PATH."/class.connections.php");
$process_status = $current_user->process_hotmail_contacts ;
global $page_array ;
if($page_array['url']['3'] == 'hotmail'){
$process_source = 'hotmail' ;
};
if($_GET['code']){
echo "Test 1 - ";
$access_token_json = get_hotmail_access_token();
echo "Test 2 - ";
if($access_token_json){
echo 'test';
update_user_meta($current_user->ID, 'hotmail_access_token', $access_token_json);
$taskurl = "/tasks";
$data = array();
$data['type'] = 'process_hotmail_contacts';
$data['lang'] = get_site_language();
$data['access_token'] = $access_token_json ;
$data['user_id'] = $current_user->ID ;
$options = array();
add_task($taskurl, $data, $options);
$process_status = 'busy' ;
$process_source = 'hotmail' ;
};
}
$ditc_title = _t("Invite connections") ;
get_header();
function in class.connections
function get_hotmail_access_token(){
global $hotmail_creds;
$fields=array(
'code'=> urlencode($hotmail_creds['auth_code']),
'client_id'=> urlencode($hotmail_creds['client_id']),
'client_secret'=> urlencode($hotmail_creds['client_secret']),
'redirect_uri'=> urlencode($hotmail_creds['redirect_uri']),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,'https://login.live.com/oauth20_token.srf');
curl_setopt($curl, CURLOPT_POST,5);
curl_setopt($curl, CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$access_token_json = $response->access_token;
return $access_token_json;
};
Is there a way to fetch Google contacts(Google Contacts API v3) with email id?
What I have done so far is :
$client_id=$this->config->item('google_access_key');
$client_secret=$this->config->item('google_secret_key');
$redirect_uri=$this->config->item('google_callback_url');
$max_results = 9999;
$auth_code = $_GET["code"];
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
$accesstoken = $response->access_token;
$url = 'https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$accesstoken;
$xmlresponse = $this->curl_file_get_contents($url);
if((strlen(stristr($xmlresponse,'Authorization required'))>0) && (strlen(stristr($xmlresponse,'Error '))>0))
{
$msg = "OOPS !! Something went wrong. Please try reloading the page.";
$newdata = array('msg' => $msg);
$this->session->set_userdata('message_session', $newdata);
redirect('admin/social_account_master');
}
$xml = new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
foreach ($xml as $title)
{
echo $title->title . "<br>";
}
To fetch email details I used:
$result = $xml->xpath('//gd:email');
But how can I match email details with respective contact details?
echo "<h3>Email Addresses:</h3>";
$xml = new SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
$result = $xml->xpath('//gd:email');
foreach ($result as $title) {
echo $title->attributes()->address . "<br>";
}
?>
i to used only email to retrive from google contacts how to retrive phone number and other detaisl
updated shopify.php
<?php
include('shopify_api_config.php');
class ShopifyClient {
public $shop_domain;
private $token;
private $api_key;
private $secret;
private $last_response_headers = null;
public function __construct($shop_domain, $token, $api_key, $secret) {
$this->name = "ShopifyClient";
$this->shop_domain = 'https://#4ef34cd22b136c1a7b869e77c8ce8b3c:#fb2b17c283a27c65e4461d0ce8e5871b#discountshop-8.myshopify.com';
$this->token = $token;
$this->api_key = '4ef34cd22b136c1a7b869e77c8ce8b3c';
$this->secret = '28cdbeb0b925bba5b8c9a60cfbb8c3cb';
$client = new ShopifyClient($shop_domain, $token, $api_key, $secret);
}
// Get the URL required to request authorization
public function getAuthorizeUrl($scope, $redirect_url='') {
$url = "http://{$this->shop_domain}/admin/oauth/authorize?client_id={$this->api_key}&scope=" . urlencode($scope);
if ($redirect_url != '')
{
$url .= "&redirect_uri=" . urlencode($redirect_url);
}
return $url;
}
// Once the User has authorized the app, call this with the code to get the access token
public function getAccessToken($code) {
// POST to POST https://SHOP_NAME.myshopify.com/admin/oauth/access_token
$url = "https://{$this->shop_domain}/admin/oauth/access_token";
$payload = "client_id={$this->api_key}&client_secret={$this->secret}&code=$code";
$response = $this->curlHttpApiRequest('POST', $url, '', $payload, array());
$response = json_decode($response, true);
if (isset($response['access_token']))
return $response['access_token'];
return '';
}
public function callsMade()
{
return $this->shopApiCallLimitParam(0);
}
public function callLimit()
{
return $this->shopApiCallLimitParam(1);
}
public function callsLeft($response_headers)
{
return $this->callLimit() - $this->callsMade();
}
public function call($method, $path, $params=array())
{
$baseurl = "https://{$this->shop_domain}/";
$url = $baseurl.ltrim($path, '/');
$query = in_array($method, array('GET','DELETE')) ? $params : array();
$payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();
$request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
// add auth headers
$request_headers[] = 'X-Shopify-Access-Token: ' . $this->token;
$response = $this->curlHttpApiRequest($method, $url, $query, $payload, $request_headers);
$response = json_decode($response, true);
if (isset($response['errors']) or ($this->last_response_headers['http_status_code'] >= 400))
throw new ShopifyApiException($method, $path, $params, $this->last_response_headers, $response);
return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;
}
public function validateSignature($query)
{
if(!is_array($query) || empty($query['signature']) || !is_string($query['signature']))
return false;
foreach($query as $k => $v) {
if($k == 'signature') continue;
$signature[] = $k . '=' . $v;
}
sort($signature);
$signature = md5($this->secret . implode('', $signature));
return $query['signature'] == $signature;
}
private function curlHttpApiRequest($method, $url, $query='', $payload='', $request_headers=array())
{
$url = $this->curlAppendQuery($url, $query);
$ch = curl_init($url);
$this->curlSetopts($ch, $method, $payload, $request_headers);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno) throw new ShopifyCurlException($error, $errno);
list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
$this->last_response_headers = $this->curlParseHeaders($message_headers);
return $message_body;
}
private function curlAppendQuery($url, $query)
{
if (empty($query)) return $url;
if (is_array($query)) return "$url?".http_build_query($query);
else return "$url?$query";
}
private function curlSetopts($ch, $method, $payload, $request_headers)
{
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT, 'ohShopify-php-api-client');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($request_headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
if ($method != 'GET' && !empty($payload))
{
if (is_array($payload)) $payload = http_build_query($payload);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $payload);
}
}
private function curlParseHeaders($message_headers)
{
$header_lines = preg_split("/\r\n|\n|\r/", $message_headers);
$headers = array();
list(, $headers['http_status_code'], $headers['http_status_message']) = explode(' ', trim(array_shift($header_lines)), 3);
foreach ($header_lines as $header_line)
{
list($name, $value) = explode(':', $header_line, 2);
$name = strtolower($name);
$headers[$name] = trim($value);
}
return $headers;
}
private function shopApiCallLimitParam($index)
{
if ($this->last_response_headers == null)
{
throw new Exception('Cannot be called before an API call.');
}
$params = explode('/', $this->last_response_headers['http_x_shopify_shop_api_call_limit']);
return (int) $params[$index];
}
}
class ShopifyCurlException extends Exception { }
class ShopifyApiException extends Exception
{
protected $method;
protected $path;
protected $params;
protected $response_headers;
protected $response;
function __construct($method, $path, $params, $response_headers, $response)
{
$this->method = $method;
$this->path = $path;
$this->params = $params;
$this->response_headers = $response_headers;
$this->response = $response;
parent::__construct($response_headers['http_status_message'], $response_headers['http_status_code']);
}
function getMethod() { return $this->method; }
function getPath() { return $this->path; }
function getParams() { return $this->params; }
function getResponseHeaders() { return $this->response_headers; }
function getResponse() { return $this->response; }
}
?>
I installed one private app in my shopify store for download csv file, after installed, when click download button, it will display like
Fatal error: Uncaught exception 'ShopifyCurlException' with message 'Could not
resolve host: http:; Host not found' in C:\xampp\htdocs\cat\lib\shopify.php:102
Stack trace: #0 C:\xampp\htdocs\cat\lib\shopify.php(67): ShopifyClient->curlHttpApiRequest('GET', 'https://http://...', Array, Array, Array)
#1 C:\xampp\htdocs\cat\index-oauth.php(26): ShopifyClient->call('GET', 'admin/orders.js...', Array)
#2 {main} thrown in C:\xampp\htdocs\cat\lib\shopify.php on line 102.
I dont know how to fix. If anybody know, please help.
Thank you!.
I had the same problem. Try this.
In your constructor pass the following argument as the $shop_domain.
https:// #apikey:#password#hostname
Replace #apiKey, #password and hostname with your values. Also leave '#' in hostname.
I.e. #yourshop.com
$shop_domain = 'https://#apikey:#password#hostname';
$token = 'your token';
$key = 'your key';
$secret = 'your secret';
$client = new ShopifyClient($shop_domain, $token, $api_key, $secret);
So, when I try and run this line of code, I'm getting the following error:
Fatal error: Call to undefined function curl_http_api_request_() in /Applications/XAMPP/xamppfiles/htdocs/CI/application/libraries/Shopify.php on line 58
Where line 58 is specifically this line:
$response = curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers);
I'm not really sure why it can't call the second function. The code is below. I've got no clue and am at a loss as to what the issue is.
class Shopify
{
public $_api_key;
public $_shared_secret;
public $CI; // To hold the CI superglobal
public function __construct ()
{
$this->_assign_libraries(); // Loads the CI superglobal and loads the config into it
// Get values from the CI config
$this->_api_key = $this->CI->config->item('api_key', 'shopify');
$this->_shared_secret = $this->CI->config->item('shared_secret', 'shopify');
}
public function shopify_app_install_url($shop_domain)
{
return "http://$shop_domain/admin/api/auth?api_key=". $this->_api_key;
}
public function shopify_is_app_installed($shop, $t, $timestamp, $signature)
{
return (md5($this->_shared_secret . "shop={$shop}t={$t}timestamp={$timestamp}") === $signature);
}
public function shopify_api_client($shops_myshopify_domain, $shops_token, $private_app=false)
{
$password = $private_app ? $this->_shared_secret : md5($this->_shared_secret.$shops_token);
$baseurl = "https://" . $this->_api_key . ":$password#$shops_myshopify_domain/";
return function ($method, $path, $params=array(), &$response_headers=array()) use ($baseurl)
{
$url = $baseurl.ltrim($path, '/');
$query = in_array($method, array('GET','DELETE')) ? $params : array();
$payload = in_array($method, array('POST','PUT')) ? stripslashes(json_encode($params)) : array();
$request_headers = in_array($method, array('POST','PUT')) ? array("Content-Type: application/json; charset=utf-8", 'Expect:') : array();
$response = curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers);
$response = json_decode($response, true);
if (isset($response['errors']) or ($response_headers['http_status_code'] >= 400))
throw new ShopifyApiException(compact('method', 'path', 'params', 'response_headers', 'response', 'shops_myshopify_domain', 'shops_token'));
return (is_array($response) and (count($response) > 0)) ? array_shift($response) : $response;
};
}
public function curl_http_api_request_($method, $url, $query='', $payload='', $request_headers=array(), &$response_headers=array())
{
$url = curl_append_query_($url, $query);
$ch = curl_init($url);
curl_setopts_($ch, $method, $payload, $request_headers);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno) throw new ShopifyCurlException($error, $errno);
list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
$response_headers = $this->curl_parse_headers_($message_headers);
return $message_body;
}
private function curl_append_query_($url, $query)
{
if (empty($query)) return $url;
if (is_array($query)) return "$url?".http_build_query($query);
else return "$url?$query";
}
private function curl_setopts_($ch, $method, $payload, $request_headers)
{
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERAGENT, 'HAC');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
if ('GET' == $method)
{
curl_setopt($ch, CURLOPT_HTTPGET, true);
}
else
{
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($request_headers)) curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
if (!empty($payload))
{
if (is_array($payload)) $payload = http_build_query($payload);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $payload);
}
}
}
private function curl_parse_headers_($message_headers)
{
$header_lines = preg_split("/\r\n|\n|\r/", $message_headers);
$headers = array();
list(, $headers['http_status_code'], $headers['http_status_message']) = explode(' ', trim(array_shift($header_lines)), 3);
foreach ($header_lines as $header_line)
{
list($name, $value) = explode(':', $header_line, 2);
$name = strtolower($name);
$headers[$name] = trim($value);
}
return $headers;
}
public function shopify_calls_made($response_headers)
{
return shopify_shop_api_call_limit_param_(0, $response_headers);
}
public function shopify_call_limit($response_headers)
{
return shopify_shop_api_call_limit_param_(1, $response_headers);
}
public function shopify_calls_left($response_headers)
{
return shopify_call_limit($response_headers) - shopify_calls_made($response_headers);
}
private function shopify_shop_api_call_limit_param_($index, $response_headers)
{
$params = explode('/', $response_headers['http_x_shopify_shop_api_call_limit']);
return (int) $params[$index];
}
/**
* Shopify::_assign_libraries()
*
* Grab everything from the CI superobject that we need
*/
public function _assign_libraries()
{
$this->CI =& get_instance();
$this->CI->load->config('shopify', TRUE);
return;
}
UPDATE:
This whole line is started off by me calling this line of code:
$shopify = $this->shopify->shopify_api_client($shops_myshopify_domain, $shops_token);
I have also updated the code above to include the entire file.
You can achieve it only by passing $this as object to anonymous function, as it has its own context:
class example {
public function trigger() {
$func = $this->func();
$func($this);
}
public function func() {
return function($obj) {
$obj->inner();
};
}
public function inner() {
die('inside inner');
}
}
$obj = new example();
$obj->trigger();
EDIT: So in response to your problem:
Change this line:
return function ($method, $path, $params=array(), &$response_headers=array()) use ($baseurl)
into this:
return function ($instance, $method, $path, $params=array(), &$response_headers=array()) use ($baseurl)
Inside anonymous function change this line:
$response = curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers);
into this:
$response = $instance->curl_http_api_request_($method, $url, $query, $payload, $request_headers, $response_headers);
Now shopify_api_client function will return you this ANONYMOUS FUNCTION with no error:
$shopify = $this->shopify->shopify_api_client($shops_myshopify_domain, $shops_token);
You need to call this function in this way:
$shopify($this->shopify, ... AND HERE THE REST OF ARGUMENTS WHICH ANONYMOUS FUNCTION REQUIRE ...);
Is it clearer now? I have never used shopify, but general way it should work is as I wrote.
If your accessing a method from outside the class you need to state it, if your accessing the method from within the class you need use $this->methodname()
<?php
class shopify_api{
...
...
...
public function curl_http_api_request_($method, $url, $query='', $payload='', $request_headers=array(), &$response_headers=array())
{
$url = curl_append_query_($url, $query);
$ch = curl_init($url);
curl_setopts_($ch, $method, $payload, $request_headers);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$error = curl_error($ch);
curl_close($ch);
if ($errno) throw new ShopifyCurlException($error, $errno);
list($message_headers, $message_body) = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
$response_headers = $this->curl_parse_headers_($message_headers);
return $message_body;
}
}
$shopify = new shopify_api();
//--------V
$response=$shopify->curl_http_api_request_();
?>
Since you have updated your question have you tried changing:
$shopify = $this->shopify->shopify_api_client($shops_myshopify_domain, $shops_token);
too: (as it seems your adding extra shopify property, i cant see from yourcode where you have set & injected your methods to it)
$shopify = $this->shopify_api_client($shops_myshopify_domain, $shops_token);