I'm attempting to hit the PageSpeed Insights API as documented here https://developers.google.com/speed/docs/insights/v5/about with PHP and curl and am getting in return a response that looks like this:
{
'captchaResult' => "CAPTCHA_NEEDED"
}
My code to query the API looks like this:
$url = 'https://google.com';
$cmd = "curl -H 'Cache-Control: no-cache' https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=$url&key={MY_KEY}";
$result = json_decode(`$cmd`, true);
The documentation states "If you plan on using the API in an automated way and making multiple queries per second, you'll need an API key". I've created an API key in the developer's console, but am still getting the above result.
Ideally, I'd like to be able to make short bursts of 10-20 requests per second.
I have two questions:
Is there any documentation available for rate limiting in the PageSpeed Insights API?
How do I satisfy the CAPTCHA_NEEDED requirement for this API?
You required to get the API key to proceed without captcha, via credentials page.
Then set up environment variable with key you got on previous step, for example:
MY_KEY=adasda5434sdsa234sasdd
set website url:
URL="https://EXAMPLE.com"
then you can use service from CLI:
curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url={$URL}&&key={$MY_KEY}
Also, you can use curl without variables:
curl https://www.googleapis.com/pagespeedonline/v5/runPagespeed?url=https://EXAMPLE.com&&key=adasda5434sdsa234sasdd
Documentation available.
Related
I am trying to set up Oauth with the YouTube Data API. I had a Laravel app which has Socialite set up. Out of the box YouTube isn't set up with this but I saw that there is a provider for YouTube here:
https://socialiteproviders.netlify.app/providers/you-tube.html
I have done all of the steps outlined on the page along with all routes that I need. I have also done the Oauth set up on Google Developer Console and got the client ID/secret key and set the callback.
When I use the login URL it works where I'm redirected for login with Google. The problem comes when the callback URL is reached. I get the error:
ErrorException
Undefined index: items
This occurs on the provider callback function which has the code:
$user = Socialite::driver('youtube')->user();
I have tried using stateless:
$user = Socialite::driver('youtube')->stateless()->user();
But get the same error. All caches have been cleared. I am pretty sure that the setup was done correctly as I'm also using the Twitch provider from https://socialiteproviders.netlify.app/providers/twitch.html which the setup was similar and it works correctly.
Please can anyone advise? Thanks.
Try selecting the fields you want to access first:
$user = Socialite::driver('youtube')->fields([
'items'
])->user();
I'm facing the same issue. Is it possible that the API has changed? If I take a look at the raw response there
I also stumbled onto this issue:
When I tested it, I did not got the error, but my colleague did so I figured it had something to do with the account that tried to connect.
I changed my approach from:
$user = Socialite::driver('youtube')->stateless()->user();
And just received tokens by doing this:
$socialite = Socialite::driver('youtube');
$code = $request->input('code');
$response = $socialite->getAccessTokenResponse($code);
$response will contain an array of tokens. I used these tokens to connect it to an existing user in my database.
I don't know if this is the solution for your workflow, but it is a way to get around the mysterious error.
The issue is due to YouTube no longer automatically creating a channel for your google/gmail account like it did in the past. This results in responses completely missing an items array.
if you dd($response->getBody()->getContents()) the response for an account that throws an error you'll see this.
I've made a pull request for this here. https://github.com/SocialiteProviders/YouTube/pull/8
I am new to integrating bitcoin API to my PHP page, I have created a bitcoin account with luno, I have created API Key.
I have been given this url to get balance using my generated API Key:
$ curl -u keyid:keysecret https://api.mybitx.com/api/1/balance
Can anybody help with a proper example on how I can use my API with this given url to display my wallet balance on a PHP page?
The easiest way I know to convert curl commands to executable PHP code is to use this site: https://incarnate.github.io/curl-to-php/
Just add your curl command:
curl -u keyid:keysecret https://api.mybitx.com/api/1/balance
to the textbox that says "Paste curl here". Then take the PHP that is output and integrate it into your app.
You can use the PHP Requests library for an easy way to make HTTP Requests. Your curl command can be converted as follows:
<?php
include('vendor/rmccue/requests/library/Requests.php');
Requests::register_autoloader();
// Use additional headers if necessary
$headers = array();
// Create authorization using key and secret.
$options = array('auth' => array('keyid', 'keysecret'));
// Create the GET request to the URL
$response = Requests::get('https://api.mybitx.com/api/1/balance', $headers, $options);
// The result is available in $response
?>
Geocode API allowed 2500 requests per day, but my site is not sending such requests in a single day. is my code sending automated requests?
function getmap($addrss,$location,$city){
$address = $addrss.' '.$city.' '.$location;
$prepAddr = urlencode($address);
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false');
$output= json_decode($geocode);
$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;
$retval = $lat.'*'.$long;
return $retval;
}
Response came
[error_message] => You have exceeded your daily request quota for this API.....[status] => OVER_QUERY_LIMIT )
Please provide any suggestion.
From the API Example I've looked up it specifically requests appending the API Key to the request URL, might that be it? Otherwise my guess would be accidental looped requests.
It would be best if the API Key was set outside this function in case other functions in this class / object want to access the API as well.
$geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false&key='.$API_KEY);
Try to use google browser key as below in order to get more number of requests per day
http://maps.google.com/maps/api/geocode/json?key=YOUR_GOOGLE_MAP_API_KEY&address=los+Angels
Follow these steps to get GOOGLE API KEY,
Go to the Google Developers Console.
Create or select a project.
Click Continue to enable the API and any related services.
On the Credentials page, get a Browser key (and set the API
Credentials).
(Optional) Enable billing.
Note: If you have an existing Browser key, you may use that key.
To prevent quota theft, secure your API key following these best practices.
In the past, using Twitter API version 1, I used the following URL to get a JSON feed of all tweets with the hashtag "baseball":
http://search.twitter.com/search.json?q=%23baseball&result_type=recent
How do you achieve a similar result using API version 1.1? I'm using PHP as my server-side code, so not sure if I need to use it to authenticate and such?
Sample code would be extremely helpful. Thanks.
As you know, authenticated requests are now required, so there's a few things that you may need to take a look at first. The new 1.1 search, how to use hashtags, and authentication.
Twitter Search for 1.1
The new twitter search api docs can be found here. According to these docs:
https://api.twitter.com/1.1/search/tweets.json is the new resource URL to use for search.
Hashtag searches
You've got that part right! %23 decodes to a # character.
Authentication
OAuth is a lot more complex. It would help if you just used a library that just worked.
Here's a post a lot of people found useful to help you make authenticated requests to the 1.1 API. This includes a one-file include library to make requests like those you require.
Example
This example assumes you're using the above library and set up your keys etc. To make your request:
// Your specific requirements
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=#baseball&result_type=recent';
// Perform the request
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
Yes, that's it. Apart from the little setting up you need to do (as my post explains), for your dev keys, that's everything you need to perform authenticated requests.
Response
The response is returned to you in JSON. From the overview:
API v1.1 will support JSON only. We've been hinting at this for some time now, first dropping XML support on the Streaming API and more recently on the trends API. We've chosen to throw our support behind the JSON format shared across the platform.
If you just want to test, you can do the follow:
Access the twitter dev console: https://dev.twitter.com/console
In Authentication put: OAuth 1, that will ask you to give permission from your twitter account.
Request URL put GET
In url: https://api.twitter.com/1.1/search/tweets.json?q=%23yourhashtag
After Send, in Request window, copy the Authorization value.
Now put it in your request header.
Go example:
func main() {
client := &http.Client{}
req, _ := http.NewRequest("GET", "https://api.twitter.com/1.1/search/tweets.json?q=%23golang", nil)
req.Header.Add("Authorization", `OAuth oauth_consumer_key=...`)
resp, _ := client.Do(req)
io.Copy(os.Stdout, resp.Body)
}
Here's a simple example in python using application-only auth using the requests API. Get keys by creating an app at https://apps.twitter.com/app/new.
api_key = ...
api_secret = ...
# https://dev.twitter.com/oauth/application-only
# The base64 stuff described there is the normal Basic Auth dance.
import requests
r = requests.post('https://api.twitter.com/oauth2/token',
auth=(api_key, api_secret),
headers={'Content-Type':
'application/x-www-form-urlencoded;charset=UTF-8'},
data='grant_type=client_credentials')
assert r.json()['token_type'] == 'bearer'
bearer = r.json()['access_token']
url = 'https://api.twitter.com/1.1/search/tweets.json?q=%23yourhashtag'
r = requests.get(url, headers={'Authorization': 'Bearer ' + bearer})
print r.json()
In API 1.0, we can use users/profile_image/:screen_name
For example : http://api.twitter.com/1/users/profile_image/EA_FIFA_FRANCE
But, it doesn't work anymore in API 1.1.
Do you have a solution, please ?
You can also get the twitter profile image by calling this kind of url :
https://twitter.com/[screen_name]/profile_image?size=original
For instance : https://twitter.com/VancityReynolds/profile_image?size=original
Got the info from this post :
https://twittercommunity.com/t/how-to-get-user-image-original-size-with-api-1-1/10187/14
The user's profile image
Okay, so you want a user's profile image. You're going to need to take a look at the twitter REST API 1.1 docs. This is a list of all the different requests you can make to their API (don't worry, I'll get to how you actually do this later on).
There are multiple ways to get the user's profile image, but the most notable one is: users/show. According to the docs for this, the users/show method:
Returns a variety of information about the user specified by the required user_id or screen_name parameter. The author's most recent Tweet will be returned inline when possible.
Well, the user profile image must be in there somewhere, correct?
Let's have a look at a typical response to a request for this information, using the users/show url (we'll use my profile as an example).
I've cut off some from the bottom, because there is a lot of data to go through. Most importantly, you'll see what you require:
This is the profile_image_url key that you need to get access to.
So, how do you do all this? It's pretty simple, actually.
Authenticated Requests
As you rightly pointed out, as of June 11th 2013 you can't make unauthenticated requests, or any to the 1.0 API any more, because it has been retired. So OAuth is the way to make requests to the 1.1 API.
I wrote a stack overflow post with an aim to help all you guys make authenticated requests to the 1.1 API with little to no effort.
When you use it, you'll get back the response you see above. Follow the posts instructions, step-by-step, and you can get the library here (you only need to include one file in your project).
Basically, the previous post explains that you need to do the following:
Create a twitter developer account
Get yourself a set of unique keys from twitter (4 keys in total).
Set your application to have read/write access
Include TwitterApiExchange.php (the library)
Put your keys in a $settings array
Choose your URL and request method (Post/Get) from the docs (I put the link above!)
Make the request, that's it!
A practical example
I'm going to assume you followed the step-by-step instructions in the above post (containing pretty colour pictures). Here's the code you would use to get what you want.
// Require the library file, obviously
require_once('TwitterAPIExchange.php');
// Set up your settings with the keys you get from the dev site
$settings = array(
'oauth_access_token' => "YOUR_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
// Chooose the url you want from the docs, this is the users/show
$url = 'https://api.twitter.com/1.1/users/show.json';
// The request method, according to the docs, is GET, not POST
$requestMethod = 'GET';
// Set up your get string, we're using my screen name here
$getfield = '?screen_name=j7mbo';
// Create the object
$twitter = new TwitterAPIExchange($settings);
// Make the request and get the response into the $json variable
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
// It's json, so decode it into an array
$result = json_decode($json);
// Access the profile_image_url element in the array
echo $result->profile_image_url;
That's pretty much it! Very simple. There's also users/lookup which effectively does the same thing, but you can:
Returns fully-hydrated user objects for up to 100 users per request, as specified by comma-separated values passed to the user_id and/or screen_name parameters.
If you ever need to get more than one user's details, use that, but as you only require one user's details, use users/show as above.
I hope that cleared things up a bit!
You say you want to use Twitter API 1.1 and yet you don't want to authenticate your requests.
Unauthenticated requests are not supported in API v1.1. So please adjust to the API change. See updates :
https://dev.twitter.com/blog/planning-for-api-v1-retirement
https://dev.twitter.com/docs/rate-limiting/1.1
You can get image from profile_image_url field of https://api.twitter.com/1.1/users/show.json request. Either a id or screen_name is required for this method. For example :
GET https://api.twitter.com/1.1/users/show.json?screen_name=rsarver
See details here https://dev.twitter.com/docs/api/1.1/get/users/show
I try the above methods to get the profile URL but it does not work for me. I think because Twitter changes API v1.1 to API v2.0.
I found a simple method to get a profile URL.
I use Twitter API v2 there User Lookup -> User by Username API part
Code Sample:
https://api.twitter.com/2/users/by/username/{user_name}?user.fields=profile_image_url
For Example:
https://api.twitter.com/2/users/by/username/TwitterDev?user.fields=profile_image_url
Of course, You should request with your Bearer Token then it properly work. For that, I recommend a platform it calls postman. It really helps for calling API.
Above example code return JSON like this:
{
"data": {
"name": "Twitter Dev",
"profile_image_url": "https://pbs.twimg.com/profile_images/1445764922474827784/W2zEPN7U_normal.jpg",
"username": "TwitterDev",
"id": "2244994945"
}
}
Additional:
If You want the Profile Image to be a higher size. Then you can put size in place of normal in the URL. For More Details read this one
Like This:
https://pbs.twimg.com/profile_images/1445764922474827784/W2zEPN7U_400x400.jpg
Give a vote to help more developers. 🍵
As the previous answers and comments point out:
Twitter API v1.0 is deprecated
Twitter API v1.1 requires OAuth
OP (#Steffi) doesn't want to authenticate
Pick any two; with all three it's a no-go. #Jimbo's answer is correct (and the proper way to do it), but excludes #3. Throwing out #1 means going back in time. But, we can throw out #2, and go directly to the source:
curl -s https://twitter.com/EA_FIFA_FRANCE |
sed -ne 's/^.*ProfileAvatar-image.*\(https:[^"]*\).*$/\1/p'
The sed command just says, find the line that contains "ProfileAvatar-image" and print the substring that looks like a quoted URL.
This is less stable than an authenticated API call, since Twitter may change their HTML at any time, but it's easier than dealing with OAuth, and no official rate limits!
The PHP translation should be straightforward.
try this
http://api.twitter.com/1/users/profile_image/{twitter_account}.xml?size=bigger
In API 1.1 the only way is to connect your application, retrieve the user by
https://dev.twitter.com/docs/api/1.1/get/users/show
and retrieve after his picture
profile_image_url
Hare is a very simple way to get Twitter Profile picture.
http://res.cloudinary.com/demo/image/twitter_name/w_300/{User_Name}.jpg
it's my Profile picutre:
Big: http://res.cloudinary.com/demo/image/twitter_name/w_300/avto_key.jpg
Small: http://res.cloudinary.com/demo/image/twitter_name/w_100/avto_key.jpg
you can regulate size by this part of URL - w_100, w_200, w_500 and etc.