Completed
Push — master ( ec9730...54c994 )
by Kittinan
02:23
created

HTTP::setCookiePath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
/*
3
 * 
4
 */
5
namespace KS\HTTP;
6
7
class HTTP {
8
  
9
  private $cookiePath = null;
10
  private $userAgent = 'Googlebot/2.1 (+http://www.google.com/bot.html)';
11
  private $timeout = 60;
12
  private $headers = array();
13
  
14
  public function __construct($cookiePath = null) {
15
    if(!empty($cookiePath)) {
16
        $this->cookiePath = $cookiePath;
17
    }
18
  }
19
  
20
  public function setUserAgent($userAgent) {
21
    $this->userAgent = $userAgent;
22
  }
23
  
24
  public function setCookiePath($path) {
25
    $this->cookiePath = $path;
26
  }
27
  
28
  public function setTimeout($timeout) {
29
    $this->timeout = $timeout;
30
  }
31
32
  /*
33
   * Method Get
34
   */
35 3
  public function get($url, $referer = null){
36 3
    $ch = curl_init();
37 3
    curl_setopt($ch, CURLOPT_URL, $url);
38 3
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
39 3
    curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
40 3
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
41 3
    curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
42 3
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
43
    
44 3
    if (!empty($referer)) {
45
        curl_setopt($ch, CURLOPT_REFERER, $referer);
46
    }
47
48 3
    if(!empty($this->cookiePath)){
49
      curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookiePath);
50
      curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookiePath);
51
    }
52
    
53 3
    if(!empty($this->headers)) {
54
      curl_setopt ($ch, CURLOPT_HTTPHEADER, $this->headers);
55
    }
56
    
57 3
    $content = curl_exec ($ch);
58 3
    curl_close ($ch);
59 3
    return $content;
60
  }
61
  
62
  /*
63
   * Method Post with Upload 
64
   */
65 3
  public function post($url, $params = null, $is_upload = false) {
66 3
    if(!empty($params)){
67 3
      if (is_array($params) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
68 3
        $query = http_build_query($params);
69 3
      } else {
70
        //Raw POST
71
        $query = $params;
72
      }
73 3
    }else{
74
      $query = '';
75
    }
76 3
    $ch = curl_init();
77 3
    $opts[CURLOPT_URL] =  $url;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$opts was never initialized. Although not strictly required by PHP, it is generally a good practice to add $opts = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
78 3
    $opts[CURLOPT_RETURNTRANSFER] = 1;
79 3
    $opts[CURLOPT_CONNECTTIMEOUT] = $this->timeout;
80 3
    $opts[CURLOPT_USERAGENT] = $this->userAgent;
81 3
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
82
    
83 3
    if(!empty($this->cookiePath)){
84
      $opts[CURLOPT_COOKIEFILE] = $this->cookiePath;
85
      $opts[CURLOPT_COOKIEJAR] = $this->cookiePath;
86
    }
87
    
88 3
    if(!empty($this->headers)) {
89
      curl_setopt ($ch, CURLOPT_HTTPHEADER, $this->headers);
90
    }
91
    
92 3
    if($is_upload){
93
      $opts[CURLOPT_POSTFIELDS] = $params;
94
    }else{
95 3
      $opts[CURLOPT_POSTFIELDS] = $query;
96
    }
97
    
98 3
    curl_setopt_array($ch, $opts);
99 3
    $result = curl_exec ($ch);
100 3
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
101 3
    curl_close ($ch);
102 3
    if ($status == 200) {
103 3
      return $result;
104
    }
105
    else {
106
      return false;
107
    }
108
  }
109
  
110
  /*
111
   * Download File
112
   */
113 3
  public function download($url, $savePath) {
114
    
115 3
    $fp = fopen ($savePath, 'w+');
116
    
117 3
    $ch = curl_init(str_replace(" ","%20",$url));//Here is the file we are downloading, replace spaces with %20
118
    
119 3
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
120 3
    curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
121 3
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
122 3
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
123 3
    curl_exec($ch); // get curl response
124
    
125
126 3
    $info = curl_getinfo($ch);
0 ignored issues
show
Unused Code introduced by
$info is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
127
    
128 3
    fclose($fp);
129 3
    curl_close($ch);
130 3
  }
131
  
132
  public function setHeaders($headers) {
133
    $this->headers = $headers;
134
  }
135
  
136
  public function getHeaders() {
137
    return $this->headers;
138
  }
139
  
140
}