I'm trying to create a laravel command to call Mercado Libre API. When I run this sample script in my htdocs from XAMPP folder, it returns the body data, but when I run from my laravel command it returns the body empty.
This is the script:
$app_id = 'xxx';
$secret_key = 'xxxx';
$meli = new Meli($app_id, $secret_key);
$access_token = 'xxxx';
$params = array('access_token' => $access_token);
$result = $meli->get('/users/me', $params, true);
This is the command
public function handle()
{
$app_id = 'xxx';
$secret_key = 'xxx';
$meli = new Meli($app_id, $secret_key);
$access_token = 'xxx';
$params = array('access_token' => $access_token);
$result = $meli->get('/users/me', $params, true);
}
EDIT
The scirpts are running in the same environment. I test and curl is callable.
if(is_callable('curl_init')){
//Code here
print_r("normal");
}
Related
I try to show the last tweet from a specif user, using the Twitter API. When I try to echo the first result I get an empty page. When I echo my JSON data is see this.
I have the following PHP script:
<?php
require "Twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$consumer_key = "";
$consumer_secret = "";
$access_token = "";
$access_token_secret = "";
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $access_token, $access_token_secret);
$content = $connection->get("account/verify_credentials");
$statuses = $connection->get("statuses/user_timeline", ["screen_name" => "NS_online"], ["lang" => "NL"]);
//var_dump($statuses);
// get your json as an associative array
$jsonArray = json_decode($statuses, true);
print_r($jsonArray);
$test = $jsonArray[0]->created_at;
echo $test;
?>
I am really struggling to work with the Instagram API. I currently have this code in a file called data.php which I want to return a JSON response which I can process in a JS file but I am getting this response failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
Here is the code:
<?php
$access_token = '********';
$client_id = '********';
$client_secret = "*******";
$user = "*******";
$endpoint = '/media/657988443280050001_25025320';
$params = array(
'access_token' => '********',
'count' => 10,
);
$signature = generate_sig($endpoint, $params, $client_secret);
var_dump("Signature: ". $signature);
$data = file_get_contents("https://api.instagram.com/v1/users/$user/media/recent/?access_token=$signature");
header('Content-Type: application/json; charset=UTF-8');
echo $data;
function generate_sig($endpoint, $params, $secret) {
$sig = $endpoint;
ksort($params);
foreach ($params as $key => $val) {
$sig .= "|$key=$val";
}
return hash_hmac('sha256', $sig, $secret, false);
}
?>
Here are my app settings. It is in Sandbox Mode and hasn't been submitted for review.
I'm trying to add the OAuth class in codeigniter but when i call it's show error
Non-existent class: OAuth
This is my code
class Food extends My_Controller
{
public function __construct(){
$this->load->library('OAuth');
}
public function get_yelp_api(){
$unsigned_url = "http://api.yelp.com/v2/business/the-waterboy-sacramento";
$consumer_key = '***********';
$consumer_secret = '**********';
$token = '***************';
$token_secret = '**************';
$token = new OAuthToken($token, $token_secret);
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();
$oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $unsigned_url);
$oauthrequest->sign_request($signature_method, $consumer, $token);
$signed_url = $oauthrequest->to_url();
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
$response = json_decode($data);
$json_string = file_get_contents($signed_url);
$result = json_decode($json_string);
echo '<pre>';
print_r($result);
echo '</pre>';
}
}
I also try to add it on this way
require_once(APPPATH.'libraries/OAuth.php');
but still got an error
Non-existent class: OAuth
you doing mistake loading a class as library load it like this
Non-existent class:OAuth
solution
just only need to load class at the top of controller like this:
require_once(APPPATH.'libraries/OAuth.php');
Using the default Tumblr v2 API, I'm able to connect to my application, and retrieve posts from my own account. However, I'd like users to be able to connect their own accounts. I've tried to figure this out but I'm not entirely sure how to use OAuth (im using this class). How is this done?
The code I'm using to retrieve dashboard posts is:
$consumerKey = 'xxx';
$consumerSecret = 'xxx';
$tumblr = new Tumblr\API\Client(
$consumerKey,
$consumerSecret
);
var_dump( $tumblr->getDashboardPosts() ); // using var_dump for testing purposes only
This code works, but it's only returning the code for my PERSONAL account.
I figured it out, thanks to Github user seejohnrun.
require_once 'include/util.php';
$consumerKey = 'XXX';
$consumerSecret = 'XXX';
$client = new Tumblr\API\Client($consumerKey, $consumerSecret);
$requestHandler = $client->getRequestHandler();
$requestHandler->setBaseUrl('https://www.tumblr.com/');
// If we are visiting the first time
if (!$_GET['oauth_verifier']) {
// grab the oauth token
$resp = $requestHandler->request('POST', 'oauth/request_token', array());
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
// tell the user where to go
echo ' GO ';
$_SESSION['t']=$data['oauth_token'];
$_SESSION['s']=$data['oauth_token_secret'];
} else {
$verifier = $_GET['oauth_verifier'];
// use the stored tokens
$client->setToken($_SESSION['t'], $_SESSION['s']);
// to grab the access tokens
$resp = $requestHandler->request('POST', 'oauth/access_token', array('oauth_verifier' => $verifier));
$out = $result = $resp->body;
$data = array();
parse_str($out, $data);
// and print out our new keys we got back
$token = $data['oauth_token'];
$secret = $data['oauth_token_secret'];
echo "token: " . $token . "<br/>secret: " . $secret;
// and prove we're in the money
$client = new Tumblr\API\Client($consumerKey, $consumerSecret, $token, $secret);
$info = $client->getUserInfo();
echo "<br/><br/>congrats " . $info->user->name . "!";
}
I successfully setup twitteroauth and posted a message. Now, when I try to post again, and every time AFTER THE FIRST successful message post, I get an error Your credentials do not allow access to this resource. (code 220);
Here's my code:';
$message = 'Test';
$apiKey = Ranger_Application_Util::getConfig('twitter_app_api_key');
$apiSecret = Ranger_Application_Util::getConfig('twitter_app_api_secret');
$consumerToken = Ranger_Application_Util::getStorageData('twitter_oauth_token');
$consumerSecret = Ranger_Application_Util::getStorageData('twitter_oauth_secret');
$twitterOauthVerifier = Ranger_Application_Util::getStorageData('twitter_oauth_verifier');
$_SESSIOM['oauth_token'] = $consumerToken;
$_SESSION['oauth_token_secret'] = $consumerSecret;
require_once 'twitter/twitteroauth.php';
$connection = new TwitterOAuth($apiKey,$apiSecret,$consumerToken,$consumerSecret);
$connection->host = "https://api.twitter.com/1.1/";
$accessToken = $connection->getAccessToken($twitterOauthVerifier);
$_SESSION['access_token'] = $accessToken;
$parameters = array('status' => $message);
$status = $connection->post('statuses/update',$parameters);
Core::dump($status);
die();
The data I retrieve from getStorageData is values stored in the database that pertain to that specific user.
Unless you are storing the information from $accessToken to your database in a piece of code you have not posted, I believe the issue may be that you are getting the access token after your initial connection, but not using the permanent access information from the access token when trying to post to the connection. So any future requests will not be using the permanent credentials and are therefore rejected. Try something like this:
$message = 'Test';
$apiKey = Ranger_Application_Util::getConfig('twitter_app_api_key');
$apiSecret = Ranger_Application_Util::getConfig('twitter_app_api_secret');
$twitterOauthVerifier = Ranger_Application_Util::getStorageData('twitter_oauth_verifier');
require_once 'twitter/twitteroauth.php';
$hasAccessToken = (
array_key_exists('oauth_token', $_SESSION) &&
array_key_exists('oauth_token_secret', $_SESSION));
if ($hasAccessToken)
{
$consumerToken = $_SESSION['oauth_token'];
$consumerSecret = $_SESSION['oauth_token_secret'];
}
else
{
$consumerToken = Ranger_Application_Util::getStorageData('twitter_oauth_token');
$consumerSecret = Ranger_Application_Util::getStorageData('twitter_oauth_secret');
$connection = new TwitterOAuth($apiKey, $apiSecret, $consumerToken, $consumerSecret);
$connection->host = "https://api.twitter.com/1.1/";
$accessToken = $connection->getAccessToken($twitterOauthVerifier);
$consumerToken = $accessToken['oauth_token'];
$consumerSecret = $accessToken['oauth_token_secret'];
$_SESSION['oauth_token'] = $consumerToken;
$_SESSION['oauth_token_secret'] = $consumerSecret;
}
$connection = new TwitterOAuth($apiKey, $apiSecret, $consumerToken, $consumerSecret);
$connection->host = "https://api.twitter.com/1.1/";
$parameters = array('status' => $message);
$status = $connection->post('statuses/update', $parameters);
Core::dump($status);
die();
You may want to store the oauth token information in your database after receiving it from getAccessToken() rather than using sessions. Maybe you have a method like Ranger_Application_Util::setStorageData('twitter_oauth_token', $consumerToken)?
This
$_SESSIOM['oauth_token'] = $consumerToken;
^
should be
$_SESSION['oauth_token'] = $consumerToken;