Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Add posability for dynamic configuration #112

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Exception for 429 status
Nikolay Murga committed Feb 13, 2016
commit f95ea7699c459b47dd3e3d1423ff73b1f042f596
60 changes: 31 additions & 29 deletions SEOstats/Helper/HttpRequest.php
Original file line number Diff line number Diff line change
@@ -15,26 +15,26 @@ class HttpRequest
{
/**
* HTTP GET/POST request with curl.
* @access public
* @param String $url The Request URL
* @param Array $postData Optional: POST data array to be send.
* @return Mixed On success, returns the response string.
* @access public
* @param String $url The Request URL
* @param Array $postData Optional: POST data array to be send.
* @return Mixed On success, returns the response string.
* Else, the the HTTP status code received
* in reponse to the request.
*/
public static function sendRequest($url, $postData = false, $postJson = false)
{
$ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
\SEOstats\SEOstats::BUILD_NO);
\SEOstats\SEOstats::BUILD_NO);

$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
));

if (false !== $postData) {
@@ -52,32 +52,34 @@ public static function sendRequest($url, $postData = false, $postJson = false)
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if (429 == (int)$httpCode) {
throw new ToManyRequestsException("To many requests");
}
return (200 == (int)$httpCode) ? $response : false;
}

/**
* HTTP HEAD request with curl.
*
* @access private
* @param String $a The request URL
* @param String $a The request URL
* @return Integer Returns the HTTP status code received in
* response to a GET request of the input URL.
*/
public static function getHttpCode($url)
{
$ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
\SEOstats\SEOstats::BUILD_NO);
\SEOstats\SEOstats::BUILD_NO);

$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_NOBODY => 1,
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_NOBODY => 1,
));

curl_exec($ch);
@@ -90,19 +92,19 @@ public static function getHttpCode($url)
public function getFile($url, $file)
{
$ua = sprintf('SEOstats %s https://github.com/eyecatchup/SEOstats',
\SEOstats\SEOstats::BUILD_NO);
\SEOstats\SEOstats::BUILD_NO);

$fp = fopen("$file", 'w');

$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_FILE => $fp,
CURLOPT_USERAGENT => $ua,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_CONNECTTIMEOUT => 30,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_MAXREDIRS => 2,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_FILE => $fp,
));

curl_exec($ch);