首页 > 解决方案 > PHP stream_context_create

问题描述

我有这个测试代码:

//$url = 'https://www.random.org/integers/?num=1000&min=1&max=100&col=1&base=10&format=plain&rnd=new';
$url = 'https://www.random.org/integers/';
$data = array('num'=>1000, 'min'=>1, 'max'=>100, 'col'=>1, 'base'=>10, 'format'=>'plain', 'rnd'=>'new');

$options = array(
    'http' => array(
        'method'  => 'GET',
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'user_agent' => ini_get('user_agent'),
        'content' => http_build_query($data),
        'proxy' => null,
        'request_fulluri' => true,
        'follow_location' => 1,
        'max_redirects'  => 20,
        'protocol_version' => 1.0,
        'timeout' =>  ini_get('default_socket_timeout'),
        'ignore_errors' => false
    ),
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo('<pre>');
echo($result);
echo('</pre>');

如果我使用第一个 url 页面可以正常工作,如果我尝试使用 stream_context_create 创建查询字符串,则响应不正确。

谁能帮我。Tks

标签: phphttp

解决方案


推荐阅读