I am doing the login using twitter oauth functionality. I am getting the following object from $client = $token->getHttpClient($config) method:
Zend_Oauth_Client Object
(
[_config:protected] => Zend_Oauth_Config Object
(
[_signatureMethod:protected] => HMAC-SHA1
[_requestScheme:protected] => header
[_requestMethod:protected] => POST
[_version:protected] => 1.0
[_callbackUrl:protected] => http://roomstayssvn.com/register/twittercallback
[_siteUrl:protected] => http://twitter.com/oauth
[_requestTokenUrl:protected] =>
[_accessTokenUrl:protected] =>
[_authorizeUrl:protected] =>
[_consumerKey:protected] => b04fuaxLR2d035FN8tTkQ
[_consumerSecret:protected] => NGPPovdXDnSpivNoMNIgA609ZJIB8GVKGgs6yEF8A
[_rsaPrivateKey:protected] =>
[_rsaPublicKey:protected] =>
[_token:protected] => Zend_Oauth_Token_Access Object
(
[_params:protected] => Array
(
[oauth_token] => 299516752-tksjJZUR7Q2gwrDRDpLOLCrYhySTGWz1SBwTKcRU
[oauth_token_secret] => 7S9R2FLuB0GT4vvy0GerThUnpkbSTeSalURib48Sx20
[user_id] => 299516752
[screen_name] => jogkunal5
)
)
.....
..... and so on
I want to print user_id and screen_name. How can I print it?
Following should work
$user_id = $client->getToken()->getParam('user_id');
$screen_name = $client->getToken()->getParam('screen_name');
Zend_Oauth_Client::__call() proxies any method of Zend_Oauth_Config.
Zend_Oauth_Config::getToken() returns Zend_Oauth_Token
Zend_Oauth_Token::getParam() gets the value for a parameter
Read the docs from here.
Its better you use a Good IDE that supports Zend framework.
Related
EDIT: See solution. Problem was in how the Twitter library was instantiated.
I'm trying to get Twitter authorization using OAuth2. I'm able to get it to work in a simple test script, but when I try to use it in my WordPress plugin it doesn't work.
I'm using The PHP League's OAuth2 Client with a library I wrote to connect it to Twitter; the test script is in the README.
The test script stores the OAuth2 state in $_SESSION; the actual app stores it in a WordPress transient. I've confirmed the integrity of the data through the pipeline:
Data from Twitter library after generating auth URL:
Array
(
[url] => https://twitter.com/i/oauth2/authorize?redirect_uri=https%3A%2F%2Fsmol.blog%2Fwp-json%2Fsmolblog%2Fv2%2Fconnect%2Fcallback%2Ftwitter&code_challenge=EV7BCVYmkvCnIlVLH6cVzrvjNloQlleAkkYwLLgg41w&code_challenge_method=S256&state=fd5824ef415aa325f1f68d3504bb16b3&scope=tweet.read%20users.read%20offline.access&response_type=code&approval_prompt=auto&client_id=MjVXMnRGVUN5Ym5lcVllcTVKZkk6MTpjaQ
[state] => fd5824ef415aa325f1f68d3504bb16b3
[verifier] => u7Zbf1gVEFZLyTgr_2Hk~i5P2pt8VgicyhZgdeO0pAyIZqhSoYqglHaIxsNRjHz0AHpwhlU1~Q
)
Data pulled from WordPress transient during the callback (stored slightly differently):
Array
(
[id] => fd5824ef415aa325f1f68d3504bb16b3
[userId] => 1
[info] => Array
(
[verifier] => u7Zbf1gVEFZLyTgr_2Hk~i5P2pt8VgicyhZgdeO0pAyIZqhSoYqglHaIxsNRjHz0AHpwhlU1~Q
)
)
Request object to Twitter's token endpoint:
GuzzleHttp\\Psr7\\Request Object
(
[method:GuzzleHttp\\Psr7\\Request:private] => POST
[requestTarget:GuzzleHttp\\Psr7\\Request:private] =>
[uri:GuzzleHttp\\Psr7\\Request:private] => GuzzleHttp\\Psr7\\Uri Object
(
[scheme:GuzzleHttp\\Psr7\\Uri:private] => https
[userInfo:GuzzleHttp\\Psr7\\Uri:private] =>
[host:GuzzleHttp\\Psr7\\Uri:private] => api.twitter.com
[port:GuzzleHttp\\Psr7\\Uri:private] =>
[path:GuzzleHttp\\Psr7\\Uri:private] => /2/oauth2/token
[query:GuzzleHttp\\Psr7\\Uri:private] =>
[fragment:GuzzleHttp\\Psr7\\Uri:private] =>
[composedComponents:GuzzleHttp\\Psr7\\Uri:private] =>
)
[headers:GuzzleHttp\\Psr7\\Request:private] => Array
(
[Host] => Array
(
[0] => api.twitter.com
)
[content-type] => Array
(
[0] => application/x-www-form-urlencoded
)
[Authorization] => Array
(
[0] => Basic [base64-encoded app id and secret redacted]
)
)
[headerNames:GuzzleHttp\\Psr7\\Request:private] => Array
(
[content-type] => content-type
[host] => Host
[authorization] => Authorization
)
[protocol:GuzzleHttp\\Psr7\\Request:private] => 1.1
[stream:GuzzleHttp\\Psr7\\Request:private] => GuzzleHttp\\Psr7\\Stream Object
(
[stream:GuzzleHttp\\Psr7\\Stream:private] => Resource id #101
[size:GuzzleHttp\\Psr7\\Stream:private] =>
[seekable:GuzzleHttp\\Psr7\\Stream:private] => 1
[readable:GuzzleHttp\\Psr7\\Stream:private] => 1
[writable:GuzzleHttp\\Psr7\\Stream:private] => 1
[uri:GuzzleHttp\\Psr7\\Stream:private] => php://temp
[customMetadata:GuzzleHttp\\Psr7\\Stream:private] => Array
(
)
)
)
Body of said request:
client_id=MjVXMnRGVUN5Ym5lcVllcTVKZkk6MTpjaQ&client_secret=[redacted]&grant_type=authorization_code&code=aTVUMDkybzdsVmExOEQ5MjdrVjVOQVZ3YTVDbUdmTXRDMktZSzBaSGFqVk5LOjE2NjUzNjc1MjIyNjg6MToxOmFjOjE&code_verifier=u7Zbf1gVEFZLyTgr_2Hk~i5P2pt8VgicyhZgdeO0pAyIZqhSoYqglHaIxsNRjHz0AHpwhlU1~Q
The error:
PHP Fatal error: Uncaught League\\OAuth2\\Client\\Provider\\Exception\\IdentityProviderException: Value passed for the token was invalid. in /var/www/html/wp-content/plugins/smolblog-wp/vendor/smolblog/oauth2-twitter/src/Twitter.php:169
I know I'm missing something stupid here. But I can't for the life of me figure out what. There's a lot more code in the actual app than there is in the test script, but I've verified the data at multiple points in the stack, including right before it's sent to Twitter. What else do I need to be testing, or is there something that I'm forgetting?
Found the error. In the test script, the redirectUri was passed into the constructor of the OAuth2 client; In the app, it was passed into the getAuthorizationUrl function. This worked for the initial call to Twitter, but (apparently) that data is also needed for the getAccessToken call. So here's the fix.
Old and busted:
new Twitter([
'clientId' => $app->env->twitterAppId ?? '',
'clientSecret' => $app->env->twitterAppSecret ?? '',
])
New hotness:
new Twitter([
'clientId' => $app->env->twitterAppId ?? '',
'clientSecret' => $app->env->twitterAppSecret ?? '',
'redirectUri' => "{$app->env->apiBase}connect/callback/twitter",
])
I'm using EHttpClient extension yii.
My Code:
$file = file_get_contents($path, true);
Yii::import('ext.EHttpClient.*');
$client = new EHttpClient('https://secure.efaxdeveloper.com/EFax_WebFax.serv', array(
'maxredirects' => 3,
'timeout' => 30,
'Content-Type' => 'application/x-www-form-urlencoded',
'adapter' => 'EHttpClientAdapterCurl'));
$client->setParameterPost(array('id'=>urlencode("2313125942"), 'xml'=>urlencode($file)));
SiteHelper::printShow($client);
$response = $client->request("POST");
var_dump($response->isSuccessful());
SiteHelper::printShow($response);
My xml data:
<?xml version="1.0"?>
<OutboundRequest>
<AccessControl>
<UserName>b23s2323</UserName>
<Password>b23s2323</Password>
</AccessControl>
<Transmission>
<TransmissionControl>
<Resolution>STANDARD</Resolution>
</TransmissionControl>
<DispositionControl>
<DispositionLevel>BOTH</DispositionLevel>
<DispositionEmails>
<DispositionEmail>
<DispositionAddress>johnmorris201615#gmail.com</DispositionAddress>
</DispositionEmail>
</DispositionEmails>
</DispositionControl>
<Recipients>
<Recipient>
<RecipientFax>8189790412</RecipientFax>
</Recipient>
</Recipients>
<Files>
<File>
<FileContents>Base64 pdf here</FileContents>
<FileType>pdf</FileType>
</File>
</Files>
</Transmission>
</OutboundRequest>
The information of my request:
EHttpClient Object
(
[config:protected] => Array
(
[maxredirects] => 3
[strictredirects] =>
[useragent] => EHttpClient
[timeout] => 30
[adapter] => EHttpClientAdapterCurl
[httpversion] => 1.1
[keepalive] =>
[storeresponse] => 1
[strict] => 1
[output_stream] =>
[encodecookies] => 1
[rfc3986_strict] =>
[content-type] => application/x-www-form-urlencoded
)
[adapter:protected] =>
[uri:protected] => EUriHttp Object
(
[_username:protected] =>
[_password:protected] =>
[_host:protected] => secure.efaxdeveloper.com
[_port:protected] => 443
[_path:protected] => /EFax_WebFax.serv
[_query:protected] =>
[_fragment:protected] =>
[_regex:protected] => Array
(
[alphanum] => [^\W_]
[escaped] => (?:%[\da-fA-F]{2})
[mark] => [-_.!~*'()\[\]]
[reserved] => [;\/?:#&=+$,]
[unreserved] => (?:[^\W_]|[-_.!~*'()\[\]])
[segment] => (?:(?:(?:[^\W_]|[-_.!~*'()\[\]])|(?:%[\da-fA-F]{2})|[:#&=+$,;])*)
[path] => (?:\/(?:(?:(?:[^\W_]|[-_.!~*'()\[\]])|(?:%[\da-fA-F]{2})|[:#&=+$,;])*)?)+
[uric] => (?:[;\/?:#&=+$,]|(?:[^\W_]|[-_.!~*'()\[\]])|(?:%[\da-fA-F]{2}))
)
[_scheme:protected] => https
)
[headers:protected] => Array
(
)
[method:protected] => GET
[paramsGet:protected] => Array
(
)
[paramsPost:protected] => Array
(
[id] => 2313125942
[xml] => The%encoded%url%xml%data
)
[cookiejar:protected] =>
[last_request:protected] =>
[last_response:protected] =>
[redirectCounter:protected] => 0
[_unmaskStatus:protected] =>
[_queryBracketsEscaped:protected] => 1
)
I already read the documentation from https://secure.efaxdeveloper.com/downloads.jsp Outbound already set all the required fields on xml data and request post. But I always get this error from efax developer API:
Post Unsuccessful!
Your request has failed due to invalid data.
Please refer to our documentation prior to resubmitting.
Error Level: User
Error Message: XML data argument was not passed.
Please contact us at 888-278-8666 if you continue to encounter errors.
Error Message : XML data argument was not passed What is wrong with my xml data?
(*Note on my request the xml post parameter is set the encoded file contents of my xml).
I used PHP efax instead.
PHP eFax is a PHP class that wraps the eFax® Developer API. Use PHP
eFax to automate the task of sending and receiving faxes
electronically from your PHP-powered web server.
Don't use urlencode. Just set the content type to application/x-www-form-urlencoded.
I'm working on PHP with Rackspace API, this is what I have used here:
$file->setContent(fopen('sites/default/files/rackspace/' . $end_element, 'r+'));
$cdnUrl = $file->getPublicUrl();
print_r($cdnUrl);
And its returning me the below mentioned structure.
Guzzle\Http\Url Object
(
[scheme:protected] => http
[host:protected] => something.r2.cf3.rackcdn.com
[port:protected] =>
[username:protected] =>
[password:protected] =>
[path:protected] => /something-abc.jpg
[fragment:protected] =>
[query:protected] => Guzzle\Http\QueryString Object
(
[fieldSeparator:protected] => &
[valueSeparator:protected] => =
[urlEncode:protected] => RFC 3986
[aggregator:protected] =>
[data:protected] => Array
(
)
)
)
What I need here is something like this:
Guzzle\Http\Url Object
(
[scheme] => http
[host] => something.r2.cf3.rackcdn.com
[port] =>
[username] =>
[password] =>
[path] => /something-abc.jpg
[fragment] =>
[query] => Guzzle\Http\QueryString Object
(
[fieldSeparator] => &
[valueSeparator] => =
[urlEncode] => RFC 3986
[aggregator] =>
[data] => Array
(
)
)
)
So that at least I can use those objects, any suggestions?
It is a Guzzle\Http\Url object, and you will not be able to access its protected or private properties. The class is defined here, so you can use any of the public methods to access its state.
You can also cast it to a string like so:
$stringUrl = (string) $url;
Or access other stuff:
$host = $url->getHost(); // something.r2.cf3.rackcdn.com
$scheme = $url->getScheme(); // http
$port = $url->getPort();
$path = $url->getPath(); // something-abc.jpg
The query is represented by another object, Guzzle\Http\QueryString:
$query = $url->getQuery();
For more info on object visibility, please consult the official docs.
I have seen many questions on StackOverflow and I have this code
use Zend\Session\Container;
class IndexController extends AbstractActionController {
public function indexAction() {
$userSession = new Container('user');
$userSession->username = 'Sandhya';
return new ViewModel();
}
}
When I am printing the $userSession container in the controller it is giving me this output
Zend\Session\Container Object (
[name:protected] => user
[manager:protected] => Zend\Session\SessionManager Object (
[defaultDestroyOptions:protected] => Array (
[send_expire_cookie] => 1
[clear_storage] =>
)
[name:protected] =>
[validatorChain:protected] =>
[config:protected] => Zend\Session\Config\SessionConfig Object (
[phpErrorCode:protected] =>
[phpErrorMessage:protected] =>
[rememberMeSeconds:protected] => 240
[serializeHandler:protected] =>
[validCacheLimiters:protected] => Array (
[0] => nocache
[1] => public
[2] => private
[3] => private_no_expire
)
[validHashBitsPerCharacters:protected] => Array (
[0] => 4
[1] => 5
[2] => 6
)
[validHashFunctions:protected] =>
[name:protected] =>
[savePath:protected] =>
[cookieLifetime:protected] => 2592000
[cookiePath:protected] =>
[cookieDomain:protected] =>
[cookieSecure:protected] =>
[cookieHttpOnly:protected] => 1
[useCookies:protected] => 1
[options:protected] => Array (
[gc_maxlifetime] => 2592000
)
)
[defaultConfigClass:protected] => Zend\Session\Config\SessionConfig
[storage:protected] => Zend\Session\Storage\SessionArrayStorage Object (
)
[defaultStorageClass:protected] => Zend\Session\Storage\SessionArrayStorage
[saveHandler:protected] =>
)
[storage:protected] => Array ( )
[flag:protected] => 2
[iteratorClass:protected] => ArrayIterator
[protectedProperties:protected] => Array (
[0] => name
[1] => manager
[2] => storage
[3] => flag
[4] => iteratorClass
[5] => protectedProperties
)
)
It means there is nothing like username...
But when I am printing the S_SESSION it gives me this output...
Array (
[__ZF] => Array (
[_REQUEST_ACCESS_TIME] => 1429081041.81
)
[user] => Zend\Stdlib\ArrayObject Object (
[storage:protected] => Array (
[username] => Sandhya
)
[flag:protected] => 2
[iteratorClass:protected] => ArrayIterator
[protectedProperties:protected] => Array (
[0] => storage
[1] => flag
[2] => iteratorClass
[3] => protectedProperties
)
)
)
There is a field username...
But when I am trying to get the $_SESSION in view it gives me the same output as above..
The problem is I am not able to get the username in both the container as well as in $_SESSION.
I need it in the controllers.
what can be the problem need help? Thank you.
I think you have to work on your configuration.
You have to setup a common SessionManager to manage handling of your session information.
Something like this:
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions($config);
$sessionManager = new SessionManager($sessionConfig);
$sessionManager->start();
Container::setDefaultManager($sessionManager);
I would suggest registering your SessionManager config in your ServiceManager instance and then use it throughout the application.
'service_manager' => array(
'factories' => array(
'session_manager' => 'My\Factory\SessionManagerFactory'
)
)
You can then get your SessionManager in any controller:
$sessionManager = $this->serviceLocator->get('session_manager');
And if you create a new Container it will use your common/default SessionManager instance automatically so all will be managed in one place.
$userSession = new Container('user');
$userSession->getManager() === $this->serviceLocator->get('session_manager') // true
On how to register your session_manager I will refer to the official ZF2 documentation.
You can use the following code:
$userSession = new Container('user');
//To check the session variable in zf2:
if($userSession->offsetExists('username')){
//Your logic after check condition
}
This will return true or false on the basis of session exist or not.
//To get the value of session:
echo $user->offsetGet('username');
Above code will return the value of session index username.
Instead of $userSession->username = 'Sandhya'; you can use below code:
$user->offsetSet('username','Sandhya');
This is zf2 standard, which is used by session container in zf2.
you can just get your username from session in controllers.
$userSession = new Container('user');
$username = $userSession->username ;
var_dump($username); //Sandhya
it work for me . try it !
Using Zend amazon service, I have fetched till there.
$url=$result->LargeImage->Url;
print_r($url);
the output of this is:
Zend_Uri_Http Object
(
[_username:protected] =>
[_password:protected] =>
[_host:protected] => ecx.images-amazon.com
[_port:protected] =>
[_path:protected] => /images/I/41voDxJS62L.jpg
[_query:protected] =>
[_fragment:protected] =>
[_regex:protected] => Array
(
[escaped] => %[[:xdigit:]]{2}
[unreserved] => [A-Za-z0-9-_.!~*'()\[\]]
[segment] => (?:%[[:xdigit:]]{2}|[A-Za-z0-9-_.!~*'()\[\]:#&=+$,;])*
[path] => (?:\/(?:(?:%[[:xdigit:]]{2}|[A-Za-z0-9-_.!~*'()\[\]:#&=+$,;])*)?)+
[uric] => (?:%[[:xdigit:]]{2}|[A-Za-z0-9-_.!~*'()\[\];\/?:#&=+$,])
)
[_scheme:protected] => http
)
We can't do echo $url->_host:protected here as 'protected' is keyword.
How do I access that?
Can anyone help please?
Thanks
Use the getter.
$result->LargeImage->Url->getHost()
or
$url->getHost()
See the documentation : http://framework.zend.com/apidoc/1.9/Zend_Uri/Zend_Uri_Http.html