具有授权的file_get_contents请求

时间:2020-10-29 13:11:06

标签: php

我想发送带有file_get_contents的获取请求。此代码返回failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found。 我该怎么解决?

<?php
define('API_KEY','eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...');

$data = http_build_query(
    array(
        'name' => 'test',
        'count' => '1000',
    )
);

$url = 'https://www.example.com/order';
$options = array('http' => array(
    'method'  => 'GET',
    'content' => $data,
    'header' =>
        "Content-Type: application/x-www-form-urlencoded\r\n".
        "Authorization: Bearer ".API_KEY
    )
);

$context  = stream_context_create($options);
$response1 = file_get_contents($url, false, $context);
print_r($response1) ;

我也尝试了此底部代码,但它再次返回错误!

$url = 'https://www.example.com/order?name=test&count=1000';
$options = array('http' => array(
    'method'  => 'GET',
    'header' =>
        "Content-Type: application/x-www-form-urlencoded\r\n".
        "Authorization: Bearer ".API_KEY
    )
);

0 个答案:

没有答案