【问题标题】:How to make a HTTP request to a REST service in PHP?如何在 PHP 中向 REST 服务发出 HTTP 请求?
【发布时间】:2015-01-26 16:59:54
【问题描述】:

我想知道是否有人可以帮助我了解如何使用这些信息来获取 JSON?我应该使用 cURL 还是 fsockopen?

GET /market/10000002/orders/buy/?type=https://api-sisi.testeveonline.com/types/683/ HTTP/1.1
Host: https://api-sisi.testeveonline.com

授权:承载 jKVB8oaN9qboU5kQG4sWSoWxzSUaFkQaUyeisy8jWU3apRfYSgYsKpZGNbLh41xXEzuy-NDBX1FohEdEadaukQ2 接受:application/vnd.ccp.eve.MarketOrderCollection-v1+json

我尝试过这样做,但我不知道这是否可行?

$fp = fsockopen("api.eveonline.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET /market/10000002/orders/buy/?    type=https://api.eveonline.com/types/683/ HTTP/1.1\r\n";
$out .= "Host: https://api.eveonline.com\r\n";
$out .= "Authorization: Bearer ".auth."\r\n\r\n";
$out .= "Accept: application/vnd.ccp.eve.MarketOrderCollection-v1+json\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
    echo fgets($fp, 128);
}
fclose($fp);
}

谢谢

编辑 1:

$opts = array('http' =>
  array(
    'method'  => 'GET',
    'header'  => "Host: https://api.eveonline.com\r\n".
    "Authorization: Bearer ".auth."\r\n".
    "Accept: application/vnd.ccp.eve.MarketOrderCollection-v1+json\r\n"
  )
);

$context  = stream_context_create($opts);
$url = 'https://api.eveonline.com/';
$result = file_get_contents($url, false, $context);

这是我仍在努力的下一次尝试。

【问题讨论】:

  • 你应该使用 cURL。有很多教程可以了解如何,包括。 PHP 手册。
  • 谢谢,我会调查的。
  • 我很喜欢 Pest 访问 REST API。
  • 谢谢halfer,我现在正在按照建议尝试file_get_contents,但如果我不能掌握它,我会试试Pest。

标签: php http curl fsockopen


【解决方案1】:

fsockopen 对于 HTTP 请求确实不是一个好的选择,因为您必须自己处理所有事情(例如压缩、分块等)。 cURL 当然是一个选项,但我最喜欢的是 file_get_contents。您需要在配置中将allow_url_fopen 设置为On,但这并不是真正的安全风险,而且功能本身非常强大。

【讨论】:

  • 我目前正在努力,有一些问题,但似乎正在慢慢实现。不确定将所有文本放在 GET 之后的位置。将很快编辑主要帖子
  • 已添加到主帖,不确定我是否正确执行此操作。
  • 您应该可以将其附加到 URL:file_get_contents('http://example.com/some/path/?a=b&amp;c=d', false, $context)
猜你喜欢
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-22
  • 1970-01-01
  • 2021-10-05
  • 2021-05-30
相关资源
最近更新 更多