How to Access the Twitter Digits API for Websites

I am working on twitter api digits to integrate it into my website, which should verify the uniqueness of the user.

Here is the link. This is the only article that officially illustrates how to implement numbers for the Internet.

In the article, I find the fact that I need to take care of the web server, unlike Digits for IOS. But there is NO INFORMATION on what to do on my web server!

What should I write in PHP for server-side programming to get a user id and phone number?

+6
source share
2 answers

In the demo, http://s.codepen.io/digits/debug/gbrgYV

after logging in, the curl command is displayed.

Use the response data to play it on the side of your server, and it will give you an answer with a phone number and ID.

Although, I do not know why, when the phone number is new, it takes some time to return the phone number to you.

+2
source

Ok, here is the full implementation:

Js

//include js <script type="text/javascript" id="digits-sdk" src="https://cdn.digits.com/1/sdk.js" async></script> <script> /* Initialize Digits for Web using your application consumer key that Fabric generated */ document.getElementById('digits-sdk').onload = function() { Digits.init({ consumerKey: '*********' }); }; /* Launch the Login to Digits flow. */ function onLoginButtonClick(phone_number=''){ if(phone_number!='') { //Digits.logIn({ Digits.embed({ phoneNumber : phone_number, container : '.my-digits-container' //remove this if u will use Digits.logIn }) .done(onLogin) /*handle the response*/ .fail(onLoginFailure); } } /* Validate and log use in. */ function onLogin(loginResponse){ // Send headers to your server and validate user by calling Digits' API var oAuthHeaders = loginResponse.oauth_echo_headers; var verifyData = { authHeader: oAuthHeaders['X-Verify-Credentials-Authorization'], apiUrl: oAuthHeaders['X-Auth-Service-Provider'], }; var request = $.ajax({ url: "<?php echo $url;?>", method: "POST", dataType: "json", data: { verifyData:verifyData, } }); request.done(function (data) { console.log(data); }); request.fail(function (jqXHR, textStatus) { alert('fail'); }); } function onLoginFailure(loginResponse){ alert('Something went wrong, Please try again'); } </script> 

PHP code

Now that you have a URL and a caption of numbers, you can use the code below:

 $apiUrl = 'https://api.digits.com/1.1/sdk/account.json'; $authHeader = 'OAuth oauth_consumer_key="**********", oauth_nonce="****", oauth_signature="****", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1481554529", oauth_token="*****", oauth_version="1.0"'; // Create a stream $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Authorization: {$authHeader}" ) ); $context = stream_context_create($opts); // Open the file using the HTTP headers set above $file = file_get_contents($apiUrl, false, $context); $final_output = array(); if($file) { $final_output = json_decode($file,true); } print_r($final_output); 
+2
source

Source: https://habr.com/ru/post/983177/


All Articles