How to handle cookies with Zend_Rest_Client? - php

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);

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.

PHP HTTP client not able to resolve web page

I am trying to use a Http client to store the HTML from a web page. The following code snippet shows how I have configured the Http client, it uses php-http/guzzle6-adapter.
I know from my tests that the client works properly when pointed at other webpages.
<?php
require_once(__DIR__.'/vendor/autoload.php');
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
use GuzzleHttp\Psr7\Request;
$config = [
'verify' => false,
'timeout' => 2
];
$adapter = GuzzleAdapter::createWithConfig($config);
$request = new Request('GET', 'https://workingwithchildren.wa.gov.au/');
// Returns a Psr\Http\Message\ResponseInterface
$response = $adapter->sendRequest($request);
echo $response->getBody();
?>
However page I am trying to resolve https://workingwithchildren.wa.gov.au/ returns the following error, no matter what I do.
The requested URL was rejected. Please consult with your administrator.
Your support ID is: 9283834035315018727
I pointed my browser at the website and used Chrome Developer Tools to examine the Request/Response data being exchanged. The screenshot below shows the Response data my browser received.
I noticed that the site is setting cookies that seem to relate to security and CPFS and I would imagine these cookies are what are stopping my client from resolving the web page successfully. But I don't know how to fix this. I'd imagine this is a problem others have faced before. Any help would be much appreciated.
For anyone experiencing a similar problem the solution I found was to, as the commenter Scuzzy suggested, add User-Agent data to my guzzle config.

API request works from local machine, not on server

I have an interesting situation when calling the Shopify API. I use the standard procedure for calling the url and get the data, like this:
define('SHOPIFY_SHOP', 'myteststore.myshopify.com');
define('SHOPIFY_APP_API_KEY', 'xxxx');
define('SHOPIFY_APP_PASSWORD', 'yyy');
$shop_url = 'https://'.SHOPIFY_APP_API_KEY.':'.SHOPIFY_APP_PASSWORD.'#'.SHOPIFY_SHOP;
$response = Requests::get($shop_url.'/admin/products.json');
And I correctly get the response, parse the data and all works great. Now, when I put it to the actual server (Ubuntu 12.04), I noticed a weird message from the Spotify API:
[API] Invalid API key or access token (unrecognized login or wrong password)
I tried creating a new app, but still its the same. So the same file and the same set works on my machine, but not on the server. (only difference in the file is the path to requests library, require_once './Requests/library/Requests.php'; for Linux and require_once '..\Requests\library\Requests.php'; for Windows) As stated, I use the requests library and I assume there has to be some trick where the library (or something else) rewrites the URl and it doesn't get to Shopify correctly.
I tried using CURL with the URL directly, and it works that way as well. Can anyone point me what might be causing this?
Update: I moved to another library which solved the issue, but would like to know what was causing this since I had great experience with Requests up to this point.
I'm starting to use the same lib, and I stumbled upon something relevant right after finding this question:
https://github.com/rmccue/Requests/issues/142#issuecomment-147276906
Quoting relevant part:
This is an intentional part of the API design; in a typical use case,
you won't necessarily need data sent along with a request. Building
the URL for you is just a convenience.
Requests::get is a helper function designed to make GET requests
lightweight in the code, which is why there's no $data parameter
there. If you need to send data, use Requests::request instead
$response = Requests::request( 'http://httpbin.org/get', $headers, $data, Requests::GET, $options );
// GET is the default for type, and $options can be blank, so this can be shortened:
$response = Requests::request( 'http://httpbin.org/get', $headers, $data );
I couldn't figure why is this happening, it appears the Requests library is stripping the parameters from GET requests, so I moved to unirest library and this solved the issue.

Guzzle Maintaining Cookies

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.

Get HTTP Response code using Zend and Curl

I have a URL that can be accessible via HTTP or HTTPS. I want to send a HEAD or GET request, which ever is fastest and get the response code so I know whether the URL is up or down.
How do I do that using Zend_HTTP_Client? I used get_headers() function, but it's very very slow on some remote servers. I am not sure if it handles HTTPS.
You may not want to use Zend_Http_Client for this - use native PHP functions instead (like fsockopen since it seems you want this to be efficient).
That said, this may work for you (and since it defaults to the socket adapter, it may not be that less efficient than using the native functions):
$client = new Zend_Http_Client();
$response = $client->setUri($uri)->request(Zend_Http_Client::HEAD);
If not, you could try setting the cURL options manually.
$adapter = new Zend_Http_Client_Adapter_Curl();
$adapter->setCurlOption(CURLOPT_NOBODY, true);
$client = new Zend_Http_Client();
$client->setAdapter($adapter);
$response = $client->setUri($uri)->request(Zend_Http_Client::HEAD);
The code's not tested. Use at your own risk.

Categories