How to authorize and fetch twitter user's timeline via api - php

I am a newbie to oAuth and twitter api, what I want to do is fetch user's timeline. I have registered my app and have got my keys. Is there any simple method to authenticate and fetch the data in php? I don't want to use any external libraries and class for now because I want to learn the internal workings too. :)
Here are my basic cURL request code:
$url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=user';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output=curl_exec($ch);
curl_close($ch);
echo $output;
But how to authenticate???

I would suggest the course of Ray Villalobos, an author at lynda.com - he tells you how to do such a thing on his website. Good luck!

Related

Shopify REST API - how to connect?

Seems like a simple task but despite having read their docs I can't figure it out. I've gotten a Storefront Access Token, as per https://help.shopify.com/en/api/storefront-api/getting-started. Then I've used it in the CURL request below. However this just returns: {"errors":"[API] Invalid API key or access token (unrecognized login or wrong password)"}
I found their documentation a bit confusing as they have quite a few different APIs, and whilst it seems that this Storefront Access Token should work for this particular API, perhaps it doesn't?
$url = "https://mysite.myshopify.com/admin/api/2019-07/products/count.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$headers = array(
'X-Shopify-Storefront-Access-Token: 2400d...........a999'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
print_r( $result);
curl_close($ch);
If you are using the Stronfront API your request should point to
https://mysite.myshopify.com/api/2019-07/products/count.json
and not to
https://mysite.myshopify.com/admin/api/2019-07/products/count.json
In addition Storefront Api request are different from the standard ones, have that in mind.

how to post php variables to other page using curl?

I'm trying to use the twitter api to make a login to my website this is my first time using any api so its really confusing for me i have read a bit online searching how to make the OAuth and found a project on git-hub which allows us to connect but this will not help me to learn php so i tried the twitter documentation and found this page. The diagram in that page shows that i should post the data to the twitter api urls again searched for on google and found a stackoverflow question but don't know how to use this is there any other tutorial or book that explains all of this? This is what i understood from reading the twitter documentation if im wrong please help me rectify my mistake
Your have to first get a request token.
Try this method, it should work (not tested) :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://api.twitter.com/oauth/request_token');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC ) ;
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$clientSecret);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($yourPost));
$response = curl_exec($ch);
Then follow the instruction of step 2 using CURL with GET

How to use Qualys API v2 with PHP?

I am trying to use the Qualys API v2 to get an xml host list returned. I think you must use cURL, but I am unfamiliar with it. Here is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_URL, "https://qualysapi.qualys.com/api/2.0/fo/asset/host/?action=list&details=Basic");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-Requested-With: Manitowoc Service Account'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec($ch);
curl_close();
$hostobj = simplexml_load_string($xml);
Actually, curl is not required for QualysGuard API calls. As long as you can make https calls you can use any method you would like. But curl is a nice framework because everything is already implemented (with perl, another alternative would be LWP).
I have not been able to find the issue with your code, but I posted a perl script that launches and downloads a Qualys report by making API requests "using WWW::Curl::Easy": https://community.qualys.com/docs/DOC-3222
I hope it could help you to write your own perl API request with libcurl.
I see nothing wrong with your code, but of course we can't see what values you are using for the username and password, and whether those credentials actually exist in QualysGuard.
To get status/error information from curl for a given request, use curl_getinfo(), curl_error(), and curl_errno() as described in the cURL Manual.

Review Board Authentication

I have a review board running on different server . I am using review board as a normal user of it and can comment on the reviews create it but i am not the admin f it. In order to view the review request or comment on it, i need to authenticate with it with my username and password. This is the access given to me. Review Board is an open source tool which is used by many organisations.
Here are the WEB API of it:- (authenticating link) http://www.reviewboard.org/docs/manual/1.5/webapi/2.0/authenticating/#logging-in
Now i am using wamp as server on my local system . I am using php as server side language. I want to use the Review Board API for fetching the data with my credientals. I am using php curl and written this code for authenticating:-
<?php
$ch = curl_init('http://SERVERIP');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Basic realm="Web API"'));
$output = curl_exec($ch);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Basic md5encryptedusernamepassword'));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
var_dump($info);
curl_close($ch);
var_dump($output);
?>
This code is not authenticating. Please have a look. Any kind of guideline will be helpful. I have spent much time understanding it. Please help.
You should set headers with key:"Authorization",value:" Basic #{Base64.encode64("username:password")}"。
Then you can access the api which is need authentication.

I can't post to Twitter using cURL and PHP

I am unable to tweet continuously in twitter - every three tweets I get:
Error posting to Twitter. Retry
How do I fix this?
My code is as follows:
$host = "http://twitter.com/statuses/update.xmlstatus=".urlencode(stripslashes(urldecode($message)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
$result = curl_exec($ch);
// Look at the returned header
$resultArray = curl_getinfo($ch);
//print_r($resultArray);
if($resultArray['http_code'] == "200"){
$twitter_status='Your message has been Tweeted';
update_tweet_extra($result_id,"1");
} else {
$twitter_status="Error posting to Twitter. Retry";
// update_tweet_extra($result_id,"0");
}
echo $twitter_status;
You can't use API with basic authentication since 31 august 2010. You can use a small spider function with curl that login in, fetch your home, and tweet. I made it:
http://www.barattalo.it/2010/09/09/how-to-change-twitter-status-with-php-and-curl-without-oauth/
If you don't want to use oAuth authentication model you have to do this way.
I don't think this is a problem with your code, but more the fact that Twitter is phasing out basic authentication support and moving to OAuth.
There's more information at http://apiwiki.twitter.com/Authentication.
This seems very odd, since status updates via the Twitter API are not rate-limited (see here), unless you're updating more than 1000 times a day.
You might work out more of what happened if you uncomment that helpful looking line:
//print_r($resultArray);
by removing the // at the beginning of line.
to tweet using twitter you will need a post_authenticity_token along with your username and password.
this token can be obtained from your profile page by fetching it using curl (after you login with curl). i experimented with curl and was able to tweet using curl. you can find my code at (though it is in bash script, it can be ported to php easily coz they both use curl)
http://pastebin.com/a5eBcEeP .
You can find a list of PHP libraries that support OAUTH and you can use to write a tweet function in PHP and the 1.1 version of the Twitter API here:
https://dev.twitter.com/docs/twitter-libraries
tmhOAuth is probably my favorite.

Categories