BAOBAB
co_chameleon_utils.class.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************************************************************************/
26 defined( 'LGV_CHAMELEON_UTILS_CATCHER' ) or die ( 'Cannot Execute Directly' ); // Makes sure that this file is in the correct context.
27 
28 /***************************************************************************************************************************/
32  /***********************************************************************************************************************/
33  /***********************/
40  static function call_curl ( $in_uri,
41  $in_post = false,
42  &$http_status = NULL,
43  &$content_failure_note = NULL
44  ) {
45  $ret = null;
46 
47  // If the curl extension isn't loaded, we're screwed.
48  if (extension_loaded('curl')) {
49  // This gets the session as a cookie.
50  if (isset($_COOKIE['PHPSESSID']) && $_COOKIE['PHPSESSID']) {
51  $strCookie = 'PHPSESSID='.$_COOKIE['PHPSESSID'].'; path=/';
52 
53  session_write_close();
54  }
55 
56  // Create a new cURL resource.
57  $resource = curl_init();
58 
59  if (isset($strCookie)) {
60  curl_setopt($resource, CURLOPT_COOKIE, $strCookie);
61  }
62 
63  // If we will be POSTing this transaction, we split up the URI.
64  if ($in_post) {
65  $spli = explode("?", $in_uri, 2);
66 
67  if (is_array($spli) && count($spli)) {
68  $in_uri = $spli[0];
69  $in_params = $spli[1];
70  // Convert query string into an array using parse_str(). parse_str() will decode values along the way.
71  parse_str($in_params, $temp);
72 
73  // Now rebuild the query string using http_build_query(). It will re-encode values along the way.
74  // It will also take original query string params that have no value and appends a "=" to them
75  // thus giving them and empty value.
76  $in_params = http_build_query($temp);
77 
78  curl_setopt($resource, CURLOPT_POST, true);
79  curl_setopt($resource, CURLOPT_POSTFIELDS, $in_params);
80  }
81  }
82 
83  // Set url to call.
84  curl_setopt($resource, CURLOPT_URL, $in_uri);
85 
86  // Make curl_exec() function (see below) return requested content as a string (unless call fails).
87  curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
88 
89  // By default, cURL prepends response headers to string returned from call to curl_exec().
90  // You can control this with the below setting.
91  // Setting it to false will remove headers from beginning of string.
92  // If you WANT the headers, see the Yahoo documentation on how to parse with them from the string.
93  curl_setopt($resource, CURLOPT_HEADER, false);
94 
95  // Set maximum times to allow redirection (use only if needed as per above setting. 3 is sort of arbitrary here).
96  curl_setopt($resource, CURLOPT_MAXREDIRS, 3);
97 
98  // Set connection timeout in seconds (very good idea).
99  curl_setopt($resource, CURLOPT_CONNECTTIMEOUT, 10);
100 
101  // Direct cURL to send request header to server allowing compressed content to be returned and decompressed automatically (use only if needed).
102  curl_setopt($resource, CURLOPT_ENCODING, 'gzip,deflate');
103 
104  // Pretend we're a browser, so that anti-cURL settings don't pooch us.
105  curl_setopt($resource, CURLOPT_USERAGENT, "cURL Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20130401 Firefox/21.0 CHAMELEON/BADGER");
106 
107  // Trust meeeee...
108  curl_setopt($resource, CURLOPT_SSL_VERIFYPEER, false);
109 
110  // Execute cURL call and return results in $content variable.
111  $content = curl_exec($resource);
112 
113  // Check if curl_exec() call failed (returns false on failure) and handle failure.
114  if (false === $content) {
115  // Cram as much info into the content note as possible.
116  $content_failure_note = "curl failure calling $in_uri, ".curl_error($resource).", ".curl_errno ($resource);
117  } else {
118  // Do what you want with returned content (e.g. HTML, XML, etc) here or AFTER curl_close() call below as it is stored in the $content variable.
119 
120  // You MIGHT want to get the HTTP status code returned by server (e.g. 200, 400, 500).
121  // If that is the case then this is how to do it.
122  $http_status = curl_getinfo($resource, CURLINFO_HTTP_CODE);
123  }
124 
125  // Close cURL and free resource.
126  curl_close ($resource);
127 
128  // Maybe echo $contents of $content variable here.
129  if (false !== $content) {
130  $ret = $content;
131  }
132  }
133 
134  return $ret;
135  }
136 };
static call_curl( $in_uri, $in_post=false, &$http_status=NULL, &$content_failure_note=NULL)
This is a function that returns the results of an HTTP call to a URI. It is a lot more secure than fi...