How to use http_get_contents method of pts_network class

Best Phoronix-test-suite code snippet using pts_network.http_get_contents

pts_network.php

Source:pts_network.php Github

copy

Full Screen

...36 public static function network_support_available()37 {38 return self::$disable_network_support == false;39 }40 public static function http_get_contents($url, $override_proxy = false, $override_proxy_port = false, $override_proxy_user = false, $override_proxy_pw = false, $http_timeout = -1)41 {42 if(!pts_network::network_support_available())43 {44 return false;45 }46 $stream_context = pts_network::stream_context_create(null, $override_proxy, $override_proxy_port, $override_proxy_user, $override_proxy_pw, $http_timeout);47 $contents = pts_file_io::file_get_contents($url, 0, $stream_context);48 return $contents;49 }50 public static function can_reach_phoronix_test_suite_com()51 {52 return pts_network::http_get_contents('http://www.phoronix-test-suite.com/PTS') == 'PTS';53 }54 public static function can_reach_openbenchmarking_org()55 {56 return pts_network::http_get_contents('http://openbenchmarking.org/PTS') == 'PTS';57 }58 public static function can_reach_phoronix_net()59 {60 return pts_network::http_get_contents('http://phoronix.net/PTS') == 'PTS';61 }62 public static function http_upload_via_post($url, $to_post_data, $supports_proxy = true)63 {64 if(!pts_network::network_support_available())65 {66 return false;67 }68 $http_parameters = array('http' => array('method' => 'POST', 'content' => http_build_query($to_post_data)));69 if($supports_proxy)70 {71 $stream_context = pts_network::stream_context_create($http_parameters);72 }73 else74 {75 $stream_context = pts_network::stream_context_create($http_parameters, false, -1, -1);76 }77 $opened_url = fopen($url, 'rb', false, $stream_context);78 $response = $opened_url ? stream_get_contents($opened_url) : false;79 // var_dump($url); var_dump($to_post_data);80 return $response;81 }82 public static function download_file($download, $to)83 {84 if(!pts_network::network_support_available())85 {86 return false;87 }88 if(strpos($download, '://') === false)89 {90 $download = 'http://' . $download;91 }92 else if(getenv('NO_HTTPS') != false)93 {94 // On some platforms like DragonFly 4.2 ran into problem of all HTTPS downloads failing95 $download = str_replace('https://', 'http://', $download);96 }97 if(PTS_IS_CLIENT && strpos(phodevi::read_property('system', 'operating-system'), ' 7') === false && function_exists('curl_init') && stripos(PTS_PHP_VERSION, 'hiphop') === false)98 {99 // XXX: RHEL/EL 7.6 PHP packages introduced a segv when using CURL... Until that's resolved, just blacklist " 7"100 // as unknown when it will be fixed, but at least there is non-CURL codepath supported fine101 // " 7" is a bit liberal but also hard due to various EL7 downstreams102 // XXX: Facebook HipHop HHVM currently seems to have problems with PHP CURL103 $return_state = pts_network::curl_download($download, $to);104 }105 else106 {107 $return_state = pts_network::stream_download($download, $to);108 }109 //echo '\nPHP CURL must either be installed or you must adjust your PHP settings file to support opening FTP/HTTP streams.\n';110 //return false;111 if($return_state == true)112 {113 pts_client::$display->test_install_progress_completed();114 }115 }116 public static function curl_download($download, $download_to, $download_port_number = false)117 {118 if(!function_exists('curl_init'))119 {120 return false;121 }122 // XXX: with curl_multi_init we could do multiple downloads at once...123 $cr = curl_init();124 $fh = fopen($download_to, 'w');125 curl_setopt($cr, CURLOPT_FILE, $fh);126 curl_setopt($cr, CURLOPT_URL, $download);127 curl_setopt($cr, CURLOPT_HEADER, false);128 curl_setopt($cr, CURLOPT_FOLLOWLOCATION, true);129 curl_setopt($cr, CURLOPT_CONNECTTIMEOUT, self::$network_timeout);130 curl_setopt($cr, CURLOPT_BUFFERSIZE, 64000);131 curl_setopt($cr, CURLOPT_USERAGENT, pts_core::codename(true));132 curl_setopt($cr, CURLOPT_CAPATH, PTS_CORE_STATIC_PATH . 'certificates/');133 curl_setopt($cr, CURLOPT_SSL_VERIFYPEER, false);134 if($download_port_number)135 {136 curl_setopt($cr, CURLOPT_PORT, $port);137 }138 if(stripos($download, 'sourceforge') === false)139 {140 // Setting the referer causes problems for SourceForge downloads141 curl_setopt($cr, CURLOPT_REFERER, 'http://www.phoronix-test-suite.com/');142 }143 if(strpos($download, 'https://openbenchmarking.org/') !== false)144 {145 curl_setopt($cr, CURLOPT_SSL_VERIFYHOST, 2);146 curl_setopt($cr, CURLOPT_CAINFO, PTS_CORE_STATIC_PATH . 'certificates/openbenchmarking-server.pem');147 }148 else if(strpos($download, 'https://www.phoromatic.com/') !== false)149 {150 curl_setopt($cr, CURLOPT_SSL_VERIFYHOST, 2);151 curl_setopt($cr, CURLOPT_CAINFO, PTS_CORE_STATIC_PATH . 'certificates/phoromatic-com.pem');152 }153 if(defined('CURLOPT_PROGRESSFUNCTION'))154 {155 // CURLOPT_PROGRESSFUNCTION only seems to work with PHP 5.3+, but is not working with HipHop HHVM ~2.0.1156 curl_setopt($cr, CURLOPT_NOPROGRESS, false);157 curl_setopt($cr, CURLOPT_PROGRESSFUNCTION, array('pts_network', 'curl_status_callback'));158 }159 if(self::$network_proxy)160 {161 curl_setopt($cr, CURLOPT_PROXY, self::$network_proxy['proxy']);162 if(!empty(self::$network_proxy['user']))163 {164 curl_setopt($cr, CURLOPT_USERPWD, self::$network_proxy['user'] . ':' . self::$network_proxy['password']);165 }166 }167 curl_exec($cr);168 curl_close($cr);169 fclose($fh);170 return true;171 }172 public static function stream_download($download, $download_to, $stream_context_parameters = null, $callback_function = array('pts_network', 'stream_status_callback'))173 {174 $stream_context = pts_network::stream_context_create($stream_context_parameters);175 if(function_exists('stream_context_set_params'))176 {177 // HHVM 2.1 doesn't have stream_context_set_params()178 stream_context_set_params($stream_context, array('notification' => $callback_function));179 }180 /*181 if(strpos($download, 'https://openbenchmarking.org/') !== false)182 {183 stream_context_set_option($stream_context, 'ssl', 'local_cert', PTS_CORE_STATIC_PATH . 'certificates/openbenchmarking-server.pem');184 }185 else if(strpos($download, 'https://www.phoromatic.com/') !== false)186 {187 stream_context_set_option($stream_context, 'ssl', 'local_cert', PTS_CORE_STATIC_PATH . 'certificates/phoromatic-com.pem');188 }189 */190 $file_pointer = @fopen($download, 'r', false, $stream_context);191 if(is_resource($file_pointer) && file_put_contents($download_to, $file_pointer))192 {193 return true;194 }195 return false;196 }197 public static function stream_context_create($parameters = null, $proxy_address = false, $proxy_port = false, $proxy_user = false, $proxy_password = false, $http_timeout = -1)198 {199 if(!is_array($parameters))200 {201 $parameters = array();202 }203 $parameters['ssl']['verify_peer'] = false;204 $parameters['ssl']['verify_peer_name'] = false;205 if($proxy_address == false && $proxy_port == false && self::$network_proxy)206 {207 $proxy_address = self::$network_proxy['address'];208 $proxy_port = self::$network_proxy['port'];209 $proxy_user = self::$network_proxy['user'];210 $proxy_password = self::$network_proxy['password'];211 }212 if($proxy_address != false && $proxy_port != false && is_numeric($proxy_port) && $proxy_port > 1)213 {214 $parameters['http']['proxy'] = 'tcp://' . $proxy_address . ':' . $proxy_port;215 $parameters['http']['request_fulluri'] = true;216 }217 if(is_numeric($http_timeout) && $http_timeout > 1)218 {219 $parameters['http']['timeout'] = $http_timeout;220 }221 else222 {223 $parameters['http']['timeout'] = self::$network_timeout;224 }225 $parameters['http']['user_agent'] = pts_core::codename(true);226 if($proxy_user != false && !empty($proxy_user))227 {228 $password = pts_strings::hex_to_str($proxy_password);229 $parameters['http']['header'] = 'Proxy-Authorization: Basic ' . base64_encode($proxy_user . ':' . $password);230 }231 else232 {233 $parameters['http']['header'] = "Content-Type: application/x-www-form-urlencoded\r\n";234 }235 $stream_context = stream_context_create($parameters);236 return $stream_context;237 }238 //239 // Callback Functions240 //241 public static function stream_status_callback($notification_code, $arg1, $message, $message_code, $downloaded, $download_size)242 {243 static $filesize = 0;244 static $last_float = -1;245 switch($notification_code)246 {247 case STREAM_NOTIFY_FILE_SIZE_IS:248 $filesize = $download_size;249 break;250 case STREAM_NOTIFY_PROGRESS:251 $downloaded_float = $filesize == 0 ? 0 : $downloaded / $filesize;252 if(abs($downloaded_float - $last_float) < 0.01)253 {254 return;255 }256 pts_client::$display->test_install_progress_update($downloaded_float);257 $last_float = $downloaded_float;258 break;259 }260 }261 private static function curl_status_callback($download_size, $downloaded)262 {263 static $last_float = -1;264 $downloaded_float = $download_size == 0 ? 0 : $downloaded / $download_size;265 if(abs($downloaded_float - $last_float) < 0.01)266 {267 return;268 }269 pts_client::$display->test_install_progress_update($downloaded_float);270 $last_float = $downloaded_float;271 }272 public static function client_startup()273 {274 if(($proxy_address = pts_config::read_user_config('PhoronixTestSuite/Options/Networking/ProxyAddress', false)) && ($proxy_port = pts_config::read_user_config('PhoronixTestSuite/Options/Networking/ProxyPort', false)))275 {276 // Don't need http:// in address and some people mistakenly do it277 // e.g. https://www.phoronix.com/forums/forum/phoronix/phoronix-test-suite/905211-problem-network-support-is-needed-to-obtain-package278 $proxy_address = str_replace(array('http://', 'https://'), '', $proxy_address);279 self::$network_proxy['proxy'] = $proxy_address . ':' . $proxy_port;280 self::$network_proxy['address'] = $proxy_address;281 self::$network_proxy['port'] = $proxy_port;282 self::$network_proxy['user'] = pts_config::read_user_config('PhoronixTestSuite/Options/Networking/ProxyUser', false);283 self::$network_proxy['password'] = pts_config::read_user_config('PhoronixTestSuite/Options/Networking/ProxyPassword', false);284 }285 else if(($env_proxy = getenv('http_proxy')) != false && count($env_proxy = pts_strings::colon_explode($env_proxy)) == 2)286 {287 self::$network_proxy['proxy'] = $env_proxy[0] . ':' . $env_proxy[1];288 self::$network_proxy['address'] = $env_proxy[0];289 self::$network_proxy['port'] = $env_proxy[1];290 self::$network_proxy['user'] = false; // TODO is there any env vars usually storing proxy user/pw?291 self::$network_proxy['password'] = false;292 }293 self::$network_timeout = pts_config::read_user_config('PhoronixTestSuite/Options/Networking/Timeout', 20);294 if(ini_get('allow_url_fopen') == 'Off')295 {296 if(!defined('PHOROMATIC_SERVER'))297 {298 echo PHP_EOL . 'The allow_url_fopen option in your PHP configuration must be enabled for network support.' . PHP_EOL . PHP_EOL;299 }300 self::$disable_network_support = true;301 }302 else if(pts_config::read_bool_config('PhoronixTestSuite/Options/Networking/NoInternetCommunication', 'FALSE'))303 {304 if(!defined('PHOROMATIC_SERVER'))305 {306 echo PHP_EOL . 'Internet Communication Is Disabled Per Your User Configuration.' . PHP_EOL . PHP_EOL;307 }308 self::$disable_internet_support = true;309 }310 else if(pts_config::read_bool_config('PhoronixTestSuite/Options/Networking/NoNetworkCommunication', 'FALSE'))311 {312 if(!defined('PHOROMATIC_SERVER'))313 {314 echo PHP_EOL . 'Network Communication Is Disabled Per Your User Configuration.' . PHP_EOL . PHP_EOL;315 }316 self::$disable_network_support = true;317 }318 319 /*320 else if(!PTS_IS_WEB_CLIENT)321 {322 $server_response = pts_network::http_get_contents('http://openbenchmarking.org/PTS', false, false);323 if($server_response != 'PTS')324 {325 // Failed to connect to PTS server326 // As a last resort, see if it can resolve IP to Google.com as a test for Internet connectivity...327 // i.e. in case Phoronix server is down or some other issue, so just see if Google will resolve328 // If google.com fails to resolve, it will simply return the original string329 if(gethostbyname('google.com') == 'google.com')330 {331 echo PHP_EOL;332 if(PTS_IS_DAEMONIZED_SERVER_PROCESS)333 {334 // Wait some seconds in case network is still coming up335 foreach(array(20, 40) as $time_to_wait)336 {337 sleep($time_to_wait);338 $server_response = pts_network::http_get_contents('http://openbenchmarking.org/PTS', false, false);339 if($server_response != 'PTS' && gethostbyname('google.com') == 'google.com')340 {341 trigger_error('没有网络连接', E_USER_WARNING);342 self::$disable_internet_support = true;343 }344 else345 {346 self::$disable_internet_support = false;347 break;348 }349 }350 }351 else352 {...

Full Screen

Full Screen

http_get_contents

Using AI Code Generation

copy

Full Screen

1$test = new pts_network;2$test = new pts_network;3$test = new pts_network;4$test = new pts_network;5$test = new pts_network;6$test = new pts_network;7$test = new pts_network;8$test = new pts_network;9$test = new pts_network;10$test = new pts_network;11$test = new pts_network;

Full Screen

Full Screen

http_get_contents

Using AI Code Generation

copy

Full Screen

1require_once('pts-network.php');2$pts_network = new pts_network();3require_once('pts-network.php');4$pts_network = new pts_network();5require_once('pts-network.php');6$pts_network = new pts_network();7require_once('pts-network.php');8$pts_network = new pts_network();9require_once('pts-network.php');10$pts_network = new pts_network();11require_once('pts-network.php');12$pts_network = new pts_network();13require_once('pts-network.php');14$pts_network = new pts_network();15require_once('pts-network.php');16$pts_network = new pts_network();17require_once('pts-network.php');18$pts_network = new pts_network();

Full Screen

Full Screen

http_get_contents

Using AI Code Generation

copy

Full Screen

1echo $contents;2echo $contents;3echo $contents;4echo $contents;5echo $contents;6echo $contents;7echo $contents;8echo $contents;9echo $contents;10echo $contents;

Full Screen

Full Screen

http_get_contents

Using AI Code Generation

copy

Full Screen

1{2 public static function http_get_contents($url)3 {4 if(function_exists('curl_init'))5 {6 $ch = curl_init();7 curl_setopt($ch, CURLOPT_URL, $url);8 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);9 curl_setopt($ch, CURLOPT_USERAGENT, 'Phoronix Test Suite');10 $data = curl_exec($ch);11 curl_close($ch);12 }13 {14 $data = file_get_contents($url);15 }16 return $data;17 }18}19{20 public static function http_post_contents($url, $post_data)21 {22 $post_data = http_build_query($post_data);23 if(function_exists('curl_init'))24 {25 $ch = curl_init();26 curl_setopt($ch, CURLOPT_URL, $url);27 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);28 curl_setopt($ch, CURLOPT_USERAGENT, 'Phoronix Test Suite');29 curl_setopt($ch, CURLOPT_POST, true);30 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);31 $data = curl_exec($ch);32 curl_close($ch);33 }34 {35 $opts = array('http' =>36 array(37 );38 $context = stream_context_create($opts);39 $data = file_get_contents($url, false, $context);40 }41 return $data;42 }43}

Full Screen

Full Screen

http_get_contents

Using AI Code Generation

copy

Full Screen

1require_once "pts_network.php";2$url = $_GET['url'];3$net = new pts_network();4$contents = $net->http_get_contents($url);5echo $contents;6require_once "pts_network.php";7$url = $_GET['url'];8$data = $_GET['data'];9$net = new pts_network();10$contents = $net->http_post_contents($url, $data);11echo $contents;12require_once "pts_network.php";13$url = $_GET['url'];14$file = $_GET['file'];15$net = new pts_network();16$contents = $net->http_post_file($url, $file);17echo $contents;18require_once "pts_network.php";19$url = $_GET['url'];20$file = $_GET['file'];21$net = new pts_network();

Full Screen

Full Screen

http_get_contents

Using AI Code Generation

copy

Full Screen

1$contents = pts_network::http_get_contents($filename);2echo $contents;3$contents = pts_network::http_get_contents($filename);4echo $contents;5$contents = pts_network::http_get_contents($filename);6echo $contents;7$contents = pts_network::http_get_contents($filename);8echo $contents;9$contents = pts_network::http_get_contents($filename);10echo $contents;11$contents = pts_network::http_get_contents($filename);12echo $contents;

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Phoronix-test-suite automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Trigger http_get_contents code on LambdaTest Cloud Grid

Execute automation tests with http_get_contents on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful