设为首页收藏本站

UNIX技术社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
热搜: console
查看: 2569|回复: 0

php file_get_contents stream_context_create 代理

[复制链接]
发表于 2013-3-15 04:34:05 | 显示全部楼层 |阅读模式
在curl很强大下。估计大家很少用file_get_contents的代理  
代理走的是tcp协议 ,做结果返回xml页面感觉好些

resource stream_context_create ([ array $options [, array $params ]] )
(PHP 4 >= 4.3.0, PHP 5)
stream_context_create — Creates a stream context

代码如下: eg:使用代理
  1. <?php
  2. $opts = array('http' => array('proxy' => 'tcp://127.0.0.1:8080', 'request_fulluri' => true));
  3. $context = stream_context_create($opts);
  4. $data = file_get_contents('http://xxxx', false, $context);
  5. echo $data;
  6. ?>
复制代码
代码如下:
eg:如何使用header
  1. <?php
  2. $opts = array( 'http-->array(
  3. 'method'=>"GET",
  4. 'header'=>"Accept-language: en\r\n" .
  5. "Cookie: foo=bar\r\n"
  6. )
  7. );
  8. $context = stream_context_create($opts);
  9. /* Sends an http request to www.jb51.net
  10. with additional headers shown above */
  11. $fp = fopen('http://xxx', 'r', false, $context);
  12. fpassthru($fp);
  13. fclose($fp);
  14. ?>
复制代码
代码如下: eg:如何使用cookie

  1. <?php
  2. $opts = array( 'http-->array(
  3. 'method'=>"GET",
  4. 'header'=>"Accept-language: en\r\n" .
  5. "Cookie: foo=bar\r\n"
  6. )
  7. );
  8. $context = stream_context_create($opts);
  9. ?>
  10. You would setup the header this way:
  11. <?php
  12. $opts = array( 'http-->array(
  13. 'method'=>"GET",
  14. 'header'=>array("Accept-language: en",
  15. "Cookie: foo=bar",
  16. "Custom-Header: value")
  17. )
  18. );
  19. $context = stream_context_create($opts);
  20. ?>
复制代码
eg:如果使用post
  1. <?php
  2. function do_post_request($url, $postdata, $files = null)
  3. {
  4. $data = "";
  5. $boundary = "---------------------".substr(md5(rand(0,32000)), 0, 10);
  6. //Collect Postdata
  7. foreach($postdata as $key => $val)
  8. {
  9. $data .= "--$boundary\n";
  10. $data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n";
  11. }
  12. $data .= "--$boundary\n";
  13. //Collect Filedata
  14. foreach($files as $key => $file)
  15. {
  16. $fileContents = file_get_contents($file['tmp_name']);
  17. $data .= "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$file['name']}\"\n";
  18. $data .= "Content-Type: image/jpeg\n";
  19. $data .= "Content-Transfer-Encoding: binary\n\n";
  20. $data .= $fileContents."\n";
  21. $data .= "--$boundary--\n";
  22. }
  23. $params = array('http' => array(
  24. 'method' => 'POST',
  25. 'header' => 'Content-Type: multipart/form-data; boundary='.$boundary,
  26. 'content' => $data
  27. ));
  28. $ctx = stream_context_create($params);
  29. $fp = fopen($url, 'rb', false, $ctx);
  30. if (!$fp) {
  31. throw new Exception("Problem with $url, $php_errormsg");
  32. }
  33. $response = @stream_get_contents($fp);
  34. if ($response === false) {
  35. throw new Exception("Problem reading data from $url, $php_errormsg");
  36. }
  37. return $response;
  38. }
  39. //set data (in this example from post)
  40. //sample data
  41. $postdata = array(
  42. 'name' => $_POST['name'],
  43. 'age' => $_POST['age'],
  44. 'sex' => $_POST['sex']
  45. );
  46. //sample image
  47. $files['image'] = $_FILES['image'];
  48. do_post_request("http://<strong>xxx</strong>", $postdata, $files);
  49. ?>
复制代码
eg:ftp
  1. <?php
  2. $opts = array('ftp' => array(
  3.     'proxy' => 'tcp://vbinprst10:8080',
  4.     'request_fulluri'=>true,
  5.     'header' => array(
  6.         "Proxy-Authorization: Basic $auth"
  7.         )
  8.     )
  9. );
  10. $context = stream_context_create($opts);
  11. $s = file_get_contents("ftp://anonymous:anonymous@ftp.example.org",false,$context);
  12. ?>
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表