If you use the proxy server and encounter an error "fopen(http://example.com): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request" note that in many situations you need also set the parameter "request_fulluri" to "true" in your stream options. Without this option the php script sends the empty request to the server as "GET / HTTP/0.0" and the proxy server replies to it with the "HTTP 400" error.
For example (working sample):
<?php
$stream = stream_context_create(Array("http" => Array("method" => "GET",
"timeout" => 20,
"header" => "User-agent: Myagent",
"proxy" => "tcp://my-proxy.localnet:3128",
'request_fulluri' => True /* without this option we get an HTTP error! */
)));
if ( $fp = fopen("http://example.com", 'r', false, $stream) ) {
print "well done";
}
?>
P>S> PHP 5.3.17
گزینههای متن HTTP
گزینههای متن HTTP — فهرست گزینههای متن HTTP
Description
گزینههای متن برای http:// و https:// .
Changelog
| Version | Description |
|---|---|
| 5.3.0 | ignore_errors اضافه شد. |
| 5.2.10 | ignore_errors اضافه شد. |
| 5.2.10 | header میتواند بصورت array اندیسدار عددی باشد. |
| 5.2.1 | timeout اضافه شد. |
| 5.1.0 | پروکسی HTTPS از طریق پروکسی HTTP اضافه شد. |
| 5.1.0 | max_redirects اضافه شد. |
| 5.1.0 | protocol_version اضافه شد. |
Examples
Example #1 دریافت یک صفحه و ارسال اطلاعات POST
<?php
$postdata = http_build_query(
array(
'var1' => 'some content',
'var2' => 'doh'
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);
?>
Example #2 نادیده گرفتن تغییر مسیر ولی گرفتن fetch header و محتوا
<?php
$url = "http://www.example.org/header.php";
$opts = array(
'http' => array('method' => 'GET',
'max_redirects' => '0',
'ignore_errors' => '1')
);
$context = stream_context_create($opts);
$stream = fopen($url, 'r', false, $context);
// header information as well as meta data
// about the stream
var_dump(stream_get_meta_data($stream));
// actual data at $url
var_dump(stream_get_contents($stream));
fclose($stream);
?>
Notes
Note: گزینههای متن جریان زیرین سوکت
گزینههای متن اضافی ممکن است توسط زیرساختار انتقال برای جریانهای http:// ارجاع دهنده به گزینههای متن برای انتقال tcp:// استفاده شود. برای جریانهای https:// به گزینههای متن برای انتقال ssl:// ارجاع کنید.
vchampion at gmail dot com ¶
7 months ago
gourav sarkar ¶
2 years ago
watch your case when using methods (POST and GET)...it must be always uppercase. in case of you write it in lower case it wont work.
