PHP错误请求错误

时间:2017-05-22 09:34:59

标签: php api get request http-headers

我尝试访问Web应用的API,该应用将以 json 格式返回数据。我们的想法是配置请求的标头以便正确读取。提供的示例请求是:

GET /links/{linkKey}/response HTTP/1.1
Access: *MY SECRET KEY*
Account: *MY ACCOUNT KEY*
Accept: application/json
Accept-Encoding: gzip
Host: localhost
User-Agent: YourApp/1.0

此示例请求应返回此示例响应:

HTTP/1.1 200 OK
Content-Type: application/json
Content-Encoding: gzip
Transfer-Encoding: chunked
Vary: Accept-Encoding

{
    "headers": [
        "column_a",
        "column_b",
        "column_c"
    ],
    "rows": [
        [
            "text 1",
            143.22,
            true
        ],
        [
            "text 2",
            98,
            false
        ],
        [
            "text 3",
            24.9,
            false
        ],
        [
            "value 4",
            242,
            false
        ],
        [
            "value 5",
            32,
            true
        ]
    ],
    "totalRows": 5
}

基于此,我编写了以下代码:

<?php
// Set header
header('Content-type: application/json');

// See https://app.example.com/#/api
$endpoint = "https://api.example.com/"; // The URL for API access
$api_key = "*MY API_KEY*";
$account_id = "*MY ACCOUNT KEY*";
$access_key = md5($account_id . $api_key);
$link_key = '*MY LINK KEY*'; //Edit your runs inside the app to get their ID

$curl_h = curl_init($endpoint);

curl_setopt($curl_h, CURLOPT_HTTPHEADER,
    array(
        "GET /links/" . $link_key . "/latest/result HTTP/1.1\r\n",
        "Access:" . $access_key . "\r\n",
        "Account:" . $account_id . "\r\n",
        "Accept: application/json\r\n",
        "Accept-Encoding: gzip\r\n",
        "Host: localhost\r\n",
        "User-Agent: CS-PHP-CLIENT/1.0\r\n",
        "Content-Type: application/json\r\n"
    )
);

// Store to variable
curl_setopt($curl_h, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($curl_h);

curl_close($curl_h);

var_dump($response);

这会在浏览器中输出 400 Bad Request 错误:

string(298) "<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at example.com Port 80</address>
</body></html>
"

从上面可以看出,提供商的规定是 access_key 必须使用md5()加密。鉴于至少出现string(298),我将代码更改为var_dump($curl_h),而不是var_dump($response)。这最终会产生一个未知的响应:

resource(2) of type (Unknown)

我将代码重写为不同的代码:

<?php
// See https://app.example.com/#/api
$endpoint = "https://api.example.com/"; // The URL for API access
$api_key = "*MY API_KEY*";
$account_id = "*MY ACCOUNT KEY*";
$access_key = md5($account_id . $api_key);
$link_key = '*MY LINK KEY*'; //Edit your runs inside the app to get their ID

// Create a stream
$opts = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"GET /links/" . $link_key . "/latest/result HTTP/1.1\r\n" .
              "Access:" . $access_key . "\r\n" .
              "Account:" . $account_id . "\r\n" .
              "Accept: application/json\r\n" .
              "Accept-Encoding: gzip\r\n" .
              "Host: localhost\r\n" .
              "User-Agent: CS-PHP-CLIENT/1.0\r\n" .
              "Content-Type: application/json\r\n"
  )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$file = file_get_contents($endpoint, false, $context);

var_dump($file);

运行此代码时,浏览器中会显示以下响应:

bool(false)

我不知道为什么这不起作用。

NB :我在我的电脑上使用localhost,正确加载其他php文件及其各自的代码。

API端点https://api.example.com/

文本编码:所有请求必须以UTF-8编码 - 并且所有响应都采用UTF-8编码。

0 个答案:

没有答案