| Conditions | 21 |
| Paths | 6144 |
| Total Lines | 133 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 34 | function getURLContent($url, $postorget = 'GET', $param = '', $followlocation = 1, $addheaders = array()) |
||
| 35 | { |
||
| 36 | //declaring of global variables |
||
| 37 | global $conf; |
||
| 38 | $USE_PROXY=empty($conf->global->MAIN_PROXY_USE)?0:$conf->global->MAIN_PROXY_USE; |
||
| 39 | $PROXY_HOST=empty($conf->global->MAIN_PROXY_HOST)?0:$conf->global->MAIN_PROXY_HOST; |
||
| 40 | $PROXY_PORT=empty($conf->global->MAIN_PROXY_PORT)?0:$conf->global->MAIN_PROXY_PORT; |
||
| 41 | $PROXY_USER=empty($conf->global->MAIN_PROXY_USER)?0:$conf->global->MAIN_PROXY_USER; |
||
| 42 | $PROXY_PASS=empty($conf->global->MAIN_PROXY_PASS)?0:$conf->global->MAIN_PROXY_PASS; |
||
| 43 | |||
| 44 | dol_syslog("getURLContent postorget=".$postorget." URL=".$url." param=".$param); |
||
| 45 | |||
| 46 | //setting the curl parameters. |
||
| 47 | $ch = curl_init(); |
||
| 48 | |||
| 49 | /*print $API_Endpoint."-".$API_version."-".$PAYPAL_API_USER."-".$PAYPAL_API_PASSWORD."-".$PAYPAL_API_SIGNATURE."<br>"; |
||
| 50 | print $USE_PROXY."-".$gv_ApiErrorURL."<br>"; |
||
| 51 | print $nvpStr; |
||
| 52 | exit;*/ |
||
| 53 | curl_setopt($ch, CURLOPT_URL, $url); |
||
| 54 | curl_setopt($ch, CURLOPT_VERBOSE, 1); |
||
| 55 | curl_setopt($ch, CURLOPT_USERAGENT, 'Dolibarr geturl function'); |
||
| 56 | |||
| 57 | @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, ($followlocation?true:false)); // We use @ here because this may return warning if safe mode is on or open_basedir is on |
||
|
1 ignored issue
–
show
|
|||
| 58 | |||
| 59 | if (count($addheaders)) curl_setopt($ch, CURLOPT_HTTPHEADER, $addheaders); |
||
| 60 | curl_setopt($ch, CURLINFO_HEADER_OUT, true); // To be able to retrieve request header and log it |
||
| 61 | |||
| 62 | // By default use tls decied by PHP. |
||
| 63 | // You can force, if supported a version like TLSv1 or TLSv1.2 |
||
| 64 | if (! empty($conf->global->MAIN_CURL_SSLVERSION)) curl_setopt($ch, CURLOPT_SSLVERSION, $conf->global->MAIN_CURL_SSLVERSION); |
||
| 65 | //curl_setopt($ch, CURLOPT_SSLVERSION, 6); for tls 1.2 |
||
| 66 | |||
| 67 | //turning off the server and peer verification(TrustManager Concept). |
||
| 68 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||
| 69 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
||
| 70 | |||
| 71 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, empty($conf->global->MAIN_USE_CONNECT_TIMEOUT)?5:$conf->global->MAIN_USE_CONNECT_TIMEOUT); |
||
| 72 | curl_setopt($ch, CURLOPT_TIMEOUT, empty($conf->global->MAIN_USE_RESPONSE_TIMEOUT)?30:$conf->global->MAIN_USE_RESPONSE_TIMEOUT); |
||
| 73 | |||
| 74 | //curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true); // PHP 5.5 |
||
| 75 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // We want response |
||
| 76 | if ($postorget == 'POST') |
||
| 77 | { |
||
| 78 | curl_setopt($ch, CURLOPT_POST, 1); // POST |
||
| 79 | curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // Setting param x=a&y=z as POST fields |
||
| 80 | } |
||
| 81 | elseif ($postorget == 'POSTALREADYFORMATED') |
||
| 82 | { |
||
| 83 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); // HTTP request is 'POST' but param string is taken as it is |
||
| 84 | curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // param = content of post, like a xml string |
||
| 85 | } |
||
| 86 | elseif ($postorget == 'PUT') |
||
| 87 | { |
||
| 88 | $array_param=null; |
||
| 89 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT' |
||
| 90 | if (! is_array($param)) parse_str($param, $array_param); |
||
| 91 | else |
||
| 92 | { |
||
| 93 | dol_syslog("parameter param must be a string", LOG_WARNING); |
||
| 94 | $array_param=$param; |
||
| 95 | } |
||
| 96 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array_param)); // Setting param x=a&y=z as PUT fields |
||
| 97 | } |
||
| 98 | elseif ($postorget == 'PUTALREADYFORMATED') |
||
| 99 | { |
||
| 100 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); // HTTP request is 'PUT' |
||
| 101 | curl_setopt($ch, CURLOPT_POSTFIELDS, $param); // param = content of post, like a xml string |
||
| 102 | } |
||
| 103 | elseif ($postorget == 'HEAD') |
||
| 104 | { |
||
| 105 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request is 'HEAD' |
||
| 106 | curl_setopt($ch, CURLOPT_NOBODY, true); |
||
| 107 | } |
||
| 108 | elseif ($postorget == 'DELETE') |
||
| 109 | { |
||
| 110 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); // POST |
||
| 111 | } |
||
| 112 | else |
||
| 113 | { |
||
| 114 | curl_setopt($ch, CURLOPT_POST, 0); // GET |
||
| 115 | } |
||
| 116 | |||
| 117 | //if USE_PROXY constant set at begin of this method. |
||
| 118 | if ($USE_PROXY) |
||
| 119 | { |
||
| 120 | dol_syslog("getURLContent set proxy to ".$PROXY_HOST. ":" . $PROXY_PORT." - ".$PROXY_USER. ":" . $PROXY_PASS); |
||
| 121 | //curl_setopt ($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); // Curl 7.10 |
||
| 122 | curl_setopt($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT); |
||
| 123 | if ($PROXY_USER) curl_setopt($ch, CURLOPT_PROXYUSERPWD, $PROXY_USER. ":" . $PROXY_PASS); |
||
| 124 | } |
||
| 125 | |||
| 126 | //getting response from server |
||
| 127 | $response = curl_exec($ch); |
||
| 128 | |||
| 129 | $request = curl_getinfo($ch, CURLINFO_HEADER_OUT); // Reading of request must be done after sending request |
||
| 130 | |||
| 131 | dol_syslog("getURLContent request=".$request); |
||
| 132 | //dol_syslog("getURLContent response =".response); // This may contains binary data, so we dont output it |
||
| 133 | dol_syslog("getURLContent response size=".strlen($response)); // This may contains binary data, so we dont output it |
||
| 134 | |||
| 135 | $rep=array(); |
||
| 136 | if (curl_errno($ch)) |
||
| 137 | { |
||
| 138 | // Ad keys to $rep |
||
| 139 | $rep['content']=$response; |
||
| 140 | |||
| 141 | // moving to display page to display curl errors |
||
| 142 | $rep['curl_error_no']=curl_errno($ch); |
||
| 143 | $rep['curl_error_msg']=curl_error($ch); |
||
| 144 | |||
| 145 | dol_syslog("getURLContent response array is ".join(',', $rep)); |
||
| 146 | } |
||
| 147 | else |
||
| 148 | { |
||
| 149 | $info = curl_getinfo($ch); |
||
| 150 | |||
| 151 | // Ad keys to $rep |
||
| 152 | $rep = $info; |
||
| 153 | //$rep['header_size']=$info['header_size']; |
||
| 154 | //$rep['http_code']=$info['http_code']; |
||
| 155 | dol_syslog("getURLContent http_code=".$rep['http_code']); |
||
| 156 | |||
| 157 | // Add more keys to $rep |
||
| 158 | $rep['content']=$response; |
||
| 159 | $rep['curl_error_no']=''; |
||
| 160 | $rep['curl_error_msg']=''; |
||
| 161 | |||
| 162 | //closing the curl |
||
| 163 | curl_close($ch); |
||
| 164 | } |
||
| 165 | |||
| 166 | return $rep; |
||
| 167 | } |
||
| 229 |
If you suppress an error, we recommend checking for the error condition explicitly: