简体   繁体   English

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

[英]how to make a http request and get response using php

I am working first time on http request and response using php so this is the request i should add in php 我是第一次使用php在http请求和响应上工作所以这是我应该在php中添加的请求

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

and i should get following response 我应该得到以下回应

{
  "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"
  ]
}

please let me know what exactly I should and how to do it 请让我知道我应该怎么做以及如何做到这一点

thanks in advance 提前致谢

You can use cURL to make http requests with PHP. 您可以使用cURL使用PHP发出http请求。 The following code makes the request you described with the correct headers. 以下代码使用正确的标头生成您描述的请求。

<?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);

?>

I'm using httpbin.org so you can check the headers and other parameters of your request. 我正在使用httpbin.org,因此您可以检查请求的标头和其他参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM