React-Native openURL Twitter Link Issues - react-native

I'm displaying a twitter feed in my app which is being pulled in from the twitter API. As the tweets don't show the full text there is a twitter short code URL (t.co) to view the full tweet.
The problem I'm having is when I click the URL using openURL it opens in a browser, which redirects to the twitter app and redirects back to the browser to display the tweet.
This works if I have the twitter app already open - if it's not open the final redirect back to the browser doesn't load the page.
Everything works fine if there's no twitter app installed it just loads in a browser.
Is there a way to avoid the redirects and just have the t.co URLs open in the twitter app if it's installed?

So I managed to solve this issue by first creating a URL unshortener in PHP:
<?php
$url = $_GET['url'];
$context = stream_context_create(
array(
'http' => array(
'follow_location' => false
)
)
);
$html = file_get_contents($url, false, $context);
$final = str_replace("location: ",'',$http_response_header[5]);
echo json_encode ($final,JSON_PRETTY_PRINT);
?>
Then from react-native fetch the full URL from the unshortener:
if (href.contains('https')) {
var short = {
getInfo() {
var myUrl = 'expander.php?url='+href
return fetch(myUrl).then((res) => res.json()).catch(function(error) {
Alert.alert(error+'');
});
}
}
short.getInfo().then((res) => {
var final = res.replace('i/web','web');
Linking.openURL(final);
})
} else {
Linking.openURL(href)
}
It was important to replace 'i/web' in the URL string with 'web' so the app opens the link natively rather than opening a mobile link.

Related

How to test url of new tab after clicking a link in laravel dusk

I'm testing my website using Laravel Dusk and I want to test a link that open a new tab to check if it goes to the correct URL. My test looks like this;
$this->browse(function (Browser $browser) {
$browser->loginAs(User::find(1))
->visit('/test')
->waitForLocation('/test')
->click('#test-link');
$window = collect($browser->driver->getWindowHandles())->last();
$browser->driver->switchTo()->window($window);
$url = $browser->driver->getCurrentURL();
Assert::assertTrue($url == 'http://foobar.com');
});
The problem here is that the URL that I'm getting is just about:blank. At first, I thought it's because the new tab is still loading but I tried to add a pause(5000) before getting current URL but it still returns about:blank. Why does the new tab doesn't proceed with the URL of the link?

After a successful login, I want to be redirected to the screen I was on before I logged in. getLoginRedirect() does not work

Suppose you were in the user list screen. From there you will go to the login screen to log in. There, you will enter your email and password and enter the submit button.
You have successfully logged in.
If the login is successful, we want to be redirected to the user list screen.
If you were in the user details screen, you will be redirected to the user details screen by
If the user was on the edit screen, the user editing screen
I have read the AuthenticationPlugin documentation at book.cakephp.org.
There I learned to use getLoginRedirect() to achieve this functionality.
I am aware that what I want to do will happen once I set up in the steps below.
However, getLoginRedirect() returns null.
What do I need to do?
What am I missing?
What's wrong?
// in Application.php
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
{
$path = $request->getPath();
$authenticationService = new AuthenticationService([
'unauthenticatedRedirect' => '/',
'queryParam' => 'redirect', // <- I believe this is the only one that matters.
]);
// Abbreviated below....
}
// in UsersController
public function login()
{
$this->request->allowMethod(['get', 'post']);
if ($this->request->is('post')) {
$result = $this->Authentication->getResult();
$requestData = $this->request->getData();
if ($result->isValid()) {
// I want to get the original url. But null is passed.
$redirect = $this->Authentication->getLoginRedirect() ?? '/';
return $this->redirect($redirect);
}
if ($this->request->is('post') && !$result->isValid()) {
$this->Flash->error(__('メールアドレス、またはパスワードが間違っています。'));
}
}
}
I think I've disclosed everything related to getLoginRedirect(). If there is something missing or something you are curious about, please feel free to let me know.
Please help me. Please help me...
The login redirect functionality provided by the plugin only works automatically when you are being redirected to the login URL after accessing a URL that requires authentication while not being logged in. In that case the authentication middleware will set the redirect query string variable with the current URL, so that the component can pick it up after the redirect to the login URL.
If you manually visit the login URL, then you'd also need to manually set the redirect query string variable, ie in your menu where you build the link that takes you to the login, add the current URL to the query string, something along the lines of this:
$service = $this->request->getAttribute('authentication');
// here `$queryParam` would by default be `redirect`
$queryParam = $service->getConfig('queryParam');
echo $this->Html->link('Login', [
'plugin' => null,
'prefix' => false,
'controller' => 'Users',
'action' => 'login',
'?' => [
$queryParam => $this->request->getRequestTarget(),
],
]);
So if you're on /users/show, the login link's URL would look something like:
/login?redirect=/users/show
and the form helper that builds your login form should pick up that exact URL, so that after submitting the form, the authentication component can read the redirect URL from the current URL accordingly.
See also
Cookbook > Views > Helpers > Html > Creating Links

Facebook Login: No redirect, white page in popup

I have a working Facebook-Login procedure using the PHP SDK. Now Facebook is requiring me to update my login flow since I apparently "violate the Facebook Platform Policies". According to them my "Login button doesn't properly reference Facebook." They suggest that "To fix this issue, you should always use our official SDK for Login." I suspect (but don't know for sure) this is because the login is not opened in a popup but rather in a new window using the PHP SDK getLoginUrl() method. Thus, I'm trying to update my Facebook Login flow.
In trying to combine the Javascript SDK (to render the official button and popup) with the PHP SDK (to handle the responses) I'm using the following approach (Source: https://developers.facebook.com/docs/php/howto/example_access_token_from_javascript)
<html>
<body>
<p>Log In with the JavaScript SDK</p>
<script>
logInWithFacebook = function() {
FB.login(function(response) {
if (response.authResponse) {
alert('You are logged in & cookie set!');
// Now you can redirect the user or do an AJAX request to
// a PHP script that grabs the signed request from the cookie.
} else {
alert('User cancelled login or did not fully authorize.');
}
});
return false;
};
window.fbAsyncInit = function() {
FB.init({
appId: 'your-app-id',
cookie: true, // This is important, it's not enabled by default
version: 'v3.2'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
</body>
</html>
When clicking the Login-Button a popup will show, as expected, to log me in. But...
...before I even enter my FB credentials the code alerts 'User cancelled login or did not fully authorize'.
...after entering my credentials the popup remains open with a blank white page.
...when I click the button while already being logged in to Facebook I will only see a blank white page in the popup.
...there's no redirect (which I suppose has to be handled in the if (response.authResponse) part)

Phantom JS load a page that loads content through JS, Return black screenshots jpg image

I am having problem to get screenshots of the website that loads through JS. I want to get the screenshots of that site but I got black screenshots .The code is working fine for other websites except this one which loads all content through js.( website is: https://signup.investorplace.com/?cid=MKT390371&eid=MKT390711&encryptedsnaid=&snaid=&step=start&assetId=AST96863)
My code is here:
var webpage = require('webpage');
var page=webpage.create();
var system=require('system');
var url='http://'+system.args[1];
page.settings.resourceTimeout = 15000; // 15 seconds
page.open(url, function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
} else {
window.setTimeout(function () {
page.render('preview.jpg',{format:'jpeg',quality:'80'});
phantom.exit();
},3000);
}
});
Notice how you create the url variable, using http protocol
var url='http://'+system.args[1];
But your target page is served via https. The url is bound to be incorrect.
When given the correct URL, PhantomJS will produce a valid preview

SMS integration in yii

I am trying to integrate SMS in my web application. I added the code in my last line of function:
header("Location:http://www.bulksmsservice.co.in");
But its not redirecting to that site. (Function is in model page)
If you use the PHP Command header, be sure there is no output before you call it. To do it in the Yii view file is too late. But to do this in the Yii Controller is not a good Practice. Better would be $this->redirect() or in the view File use Javascript Code:
<script>
window.location ='http://www.bulksmsservice.co.in';
</script>
i added my function sendbulk in controller and replace code with below
function sendbulk($num,$msg)
{
$link = "http://www.bulksmsservice.co.in/api/sentsms.php?username=****&api_password=*******&to=".$no."&priority=2&sender=******&message=".$msg. "&unicode=1";
$ctx = stream_context_create(array(
'http' => array(
'timeout' => 1
)
)
);
file_get_contents($link, 0, $ctx);
}
Now it's working perfectly for single messages and multiple messages :)