Guzzle Maintaining Cookies - php

I'm creating an API for a site that allows the user to login via the API. I'm using Guzzle, but the question is how do I use the Cookies Plugin with Guzzle? In cURL I can use a cookie file, and pass this along with requests. But the example on the Guzzle docs looks confusing.
use Guzzle\Http\Client;
use Guzzle\Plugin\Cookie\CookiePlugin;
use Guzzle\Plugin\Cookie\CookieJar\ArrayCookieJar;
$cookiePlugin = new CookiePlugin(new ArrayCookieJar());
// Add the cookie plugin to a client
$client = new Client('http://www.test.com/');
$client->addSubscriber($cookiePlugin);
// Send the request with no cookies and parse the returned cookies
$client->get('http://www.yahoo.com/')->send();
// Send the request again, noticing that cookies are being sent
$request = $client->get('http://www.yahoo.com/');
$request->send();
echo $request;
It seems to be making 3 requests. I don't understand why it's making a request to test.com, then twice to yahoo.com. Why are you not able to make 1 request?

It's just an example... You don't have to make three requests. Just attach the cookie plugin to your client and you're good.

Related

How to get session cookies of a site?

I am trying to parse a website where I have to first the cookies and then pass to another request. In Python I can do the following:
session = requests.Session()
response = session.get('https://www.page.com')
cookies = response.cookies
response = session.get('https://www.page.com/part/1234', headers=headers,cookies=cookies)
print(response.text)
What options do I have available if I use the file_get_contents or Requests library?
I could not find it in this library.

Laravel - redirect request to external url and return its response as response

i'm building an app using Laravel for providing some API for an android application, and i'm using some external APIs from another server (with another URL). i want to make something like proxy or tunnel for external API requests from android side but in my own URL.
for example:
android wants to request for externalUrl.com/api/objects but i want he request to this myDOmain.com/api/x/objects and get the exact same response that the first link returns, without any change.
and there is more than one external API, and i don't want to write separate code for each one.
need something like this :
Route::any('/x/{somewhere}', function($request){
return [$request, externalUrl.com/api/{somewhere}]->response;
})
i'm not asking for http request libraries! i want to redirect request to another domain and return its request.
Best option is to install Guzzle. https://github.com/guzzle/guzzle
It's really simple to use it.
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'externalUrl.com/api/{somewhere}');
return $response->getBody();
You can use Guzzle :
use Guzzle\Http\Client;
use Guzzle\Stream\PhpStreamRequestFactory;
[...]
$request = new Client("externalUrl.com/api/{$somewhere}");
$response = $request->send();
return $response->getBody();

How to create a Webhook server like Telegram Webhook

I need to create a Webhook server like Telegram Webhook server.
I googled it but didn't find any resources!
I'm not talking about receiving Webhook requests. I'm talking about creating a complete Webhook server to send HTTP POST requests to specific URLs. And our clients could receive the requests in their URLs by :
$response = file_get_contents('php://input');
Any helps would be great appreciated.
P.S:
Sorry for my bad English.
you can try Captain Hook laravel package, which provides you to add webhook to your laravel application
What a webhook actually does is nothing more than sending a request. The most easy way to set this up is by using Guzzle (https://packagist.org/packages/guzzlehttp/guzzle).
What you need to set up is on your side a script which decides what url to call, when that happens, just create the post request via guzzle.
$postData = [];
$client = new GuzzleHttp\Client();
$response = $client->request('POST', $url, $postData);

How to use get_file_contents in PHP when authorization is needed for URI?

I'm making a request to retrieve a JSON file to a server at a particular secure DocuSign uri. However, unless I put in the authorization information (which I do have), I am unable to have the file returned.
<?php
$json = file_get_contents("https://example.docusign.com/sensitiveIDs/moreID");
echo $json
?>
Where would I put in authorization information for the specific server/username/password/other info needed to access the particular DocuSign server using a method like this in PHP? Is there a better method to use for this scenario in PHP?
It depends on how the authorization is implemented. If its basic or digest HTTP authentication then specify it in the URL:
file_get_contents("https://$USER:$PASSWORD#example.docusign.com/sensitiveIDs/moreID");
Cookie based authentication is a lot more difficult (and probably easier to use Curl or even a more complex system like Guzzle. If its oauth2, then you probably want an oauth2 library.
Your call needs to include authentication to make the GET call to retrieve the file.
If your app is initiated by a human use Oauth to retrieve access and refresh tokens. Then included the access token with the GET request.
If your app is a "system app" that wants to autonomously retrieve the file, then you should authenticate by using X-DocuSign-Authentication -- include the following header in your HTTPS request. Since the request is HTTPS, the content is encrypted on the wire:
X-DocuSign-Authentication: <DocuSignCredentials><Username>{name}</Username><Password>{password}</Password><IntegratorKey>{integrator_key}</IntegratorKey></DocuSignCredentials>
Replace {name} with your email address (no braces), etc.
The bottom line is that you can't use the file_get_contents Php method. Instead, you'd do something like the following:
Use https://github.com/rmccue/Requests or a similar library to help with the https request. (http is not allowed due to security issues.)
(untested code)
$url = $base_url . $the_url_section_for_this_call
$headers = array('X-DocuSign-Authentication' =>
'<DocuSignCredentials><Username>your_name</Username><Password>your_password</Password><IntegratorKey>your_integrator_key</IntegratorKey></DocuSignCredentials>');
$request = Requests::get($url, $headers);
# Check that the call succeeded (either 200 or 201 depending on the method
$status_code = $request->status_code;
if ($status_code != 200 && $status_code != 201) {
throw new Exception('Problem while calling DocuSign');
}
$json = $request->body;

How to handle cookies with Zend_Rest_Client?

I have been using Curl for making network calls and I can use the COOKIEJAR option to use cookies for my requests.
Recently I've started using Zend_REST_Client, I am not sure, how to do this? Can some one show what's the best way to this?
Zend_Http_Client can handle cookie jar using Zend_Http_CookieJar. I think Zend_REST_Client relies on Zend_Http_Client, so you should be able to use a cookie jar.
$client = new Zend_Http_Client();
$cookieJar = new Zend_Http_CookieJar();
$client->setCookieJar($cookieJar);
Zend_Rest_Client::setHttpClient($httpClient);

Categories