如何使用PHP发出http请求并获得响应

时间:2016-06-02 08:42:06

标签: php http

我第一次使用php在http请求和响应上工作 所以这是我应该在php中添加的请求

GET /items?$filter=ia_itemid+eq+5 HTTP/1.1
Authorization: IC-TOKEN Credential=127896438762198746123
Host: https://api.certicasolutions.com
Accept: */*

我应该得到以下回复

{
  "completed": true,
  "totalItems": 1,
  "items": [
    {
      "ia_biserial": "",
      "ia_bloomstaxonomy": "Understanding",
      "ia_contentfocus": "",
      "ia_correctanswer": "A",
      "ia_difficulty": "Medium",
      "ia_dok": "I",
      "ia_gradelevel": "Grade 04",
      "ia_hasimages": "True",
      "ia_itemid": 5,
      "ia_pointvalue": "1",
      "ia_pvalue": "",
      "ia_subject": "Language Arts",
      "ia_teitype": "MC",
      "ia_vendorid": "i-274619",
      "passages": [
        {
          "pa_author": "Susie Post-Rust",
          "pa_commissionedstatus": "Prev. Published",
          "pa_contentarea": "N/A",
          "pa_copyrightowner": "National Geographic Society",
          "pa_copyrightyear": "2002",
          "pa_ethnicity": "Unknown",
          "pa_fleschkincaid": "3.9",
          "pa_gender": "Female(s)",
          "pa_lexile": "780L",
          "pa_multicultural": "No",
          "pa_passageid": "4",
          "pa_passagesource": "Certica Solutions, Inc. Items",
          "pa_passagesourcetitle": "World Dare to Explore Magazine",
          "pa_passagestimulus": "Reading",
          "pa_passagetitle": "Trail Mix",
          "pa_textsubtype": "Other",
          "pa_texttype": "Literary Nonfiction",
          "pa_vendorid": "p-2723",
          "pa_wordcount": "401-600"
        }
      ],
      "standards": [
        {
          "std_code": "CCSS.ELA-Literacy.L.4.5c",
          "std_document": "CC",
          "std_subject": "ELA"
        },
        {
          "std_code": "4.14",
          "std_document": "MA",
          "std_subject": "ELA"
        },
        {
          "std_code": "R.3.b",
          "std_document": "NY",
          "std_subject": "ELA"
        }
      ]
    }
  ],
  "itemFields": [
    "ia_biserial",
    "ia_bloomstaxonomy",
    "ia_contentfocus",
    "ia_correctanswer",
    "ia_difficulty",
    "ia_dok",
    "ia_gradelevel",
    "ia_hasimages",
    "ia_itemid",
    "ia_pointvalue",
    "ia_pvalue",
    "ia_subject",
    "ia_teitype",
    "ia_vendorid",
    "passages",
    "standards"
  ],
  "passageFields": [
    "pa_author",
    "pa_commissionedstatus",
    "pa_contentarea",
    "pa_copyrightowner",
    "pa_copyrightyear",
    "pa_ethnicity",
    "pa_fleschkincaid",
    "pa_gender",
    "pa_lexile",
    "pa_multicultural",
    "pa_passageid",
    "pa_passagesource",
    "pa_passagesourcetitle",
    "pa_passagestimulus",
    "pa_passagetitle",
    "pa_textsubtype",
    "pa_texttype",
    "pa_vendorid",
    "pa_wordcount"
  ],
  "standardFields": [
    "std_code",
    "std_document",
    "std_subject"
  ]
}

请让我知道我应该做什么以及如何做到

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用cURL通过PHP发出http请求。以下代码使用正确的标题生成您所描述的请求。

<?php
$crl = curl_init("https://httpbin.org/get?\$filter=ia_itemid+eq+5");

$header = array();
$header[] = 'Authorization: IC-TOKEN Credential=127896438762198746123';
$header[] = 'Accept: */*';

curl_setopt($crl, CURLOPT_HTTPHEADER,$header);
$rest = curl_exec($crl);

curl_close($crl);

print_r($rest);

?>

我正在使用httpbin.org,因此您可以检查请求的标头和其他参数。

相关问题