发布 API 问题后尝试从 api 获取 [英] Trying to GET from api after posting API issues

查看:19
本文介绍了发布 API 问题后尝试从 api 获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

开发者,我又来提一个问题了,

我正在尝试从该网站获取一些数据

希望有人能帮助我.

解决方案

有趣的问题.我有点惊讶该站点会在浏览器甚至 cURL 中接受 AJAX 请求,但在 PhantomJS 中则不然.在这种情况下,您必须非常仔细地研究和复制请求,因为其中一个小细节可能会极大地影响服务器的响应.

事实证明,这是一个必须相应设置的 cookie 和表单内容类型.

var webPage = require('webpage');var page = webPage.create();//礼貌 http://stackoverflow.com/a/1714899/2715393var 序列化 = 函数(对象){var str = [];for(var p in obj)如果(obj.hasOwnProperty(p)){str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));}返回 str.join("&");}变量设置 = {操作:POST",编码:utf8",标题:{"accept-encoding" : "identity",//https://github.com/ariya/phantomjs/issues/10930#issuecomment-81541618"x-requested-with" : "XMLHttpRequest","accept-language" : "en;q=0.8,en-US;q=0.6","authority" : "www.iamsterdam.com","accept":"application/json, text/javascript, */*; q=0.01","content-type" : "application/x-www-form-urlencoded; charset=UTF-8",cookie":网站#lang=nl"},数据:序列化({类型: '',日期过滤器:'03112016',DayPart: '',搜索词: '',邻居:'',日范围:'',视图模式:1,LastMinuteTickets : '',PageId: '3418a37d-b907-4c80-9d67-9fec68d96568',跳过:0,取:12})};page.open('https://www.iamsterdam.com/api/AgendaApi/',设置,功能(状态){控制台日志(页面内容);幻影.退出();});

Hi developers I am back again with a question,

I am trying to get some data from this website https://www.iamsterdam.com/nl/uit-in-amsterdam/uit/agenda. First did I crawl the website but when doing that it came to mind that they have a api and that will be a lot faster. So I tried to get the data from the api I tried this:

get-website.js:

var webPage = require('webpage');
var page = webPage.create();
var settings = {
  operation: "POST",
  encoding: "utf8",
  headers: {
    "Content-Type": "application/json"
  },
  data: JSON.stringify({
    DateFilter: 03112016,
    PageId: "3418a37d-b907-4c80-9d67-9fec68d96568",
    Take: 2,
    Skip: 12,
    ViewMode: 1
  })
};

page.open('https://www.iamsterdam.com/api/AgendaApi/', settings, function(status) {
  console.log(page.content);
  phantom.exit();
});

get-website.php

$phantom_script= 'get-website.js'; 

$response =  exec ('phantomjs ' . $phantom_script);

echo  $response;

But what I get back doesn't look good:

Message":"An error has occurred.","ExceptionMessage":"Page could not be found","ExceptionType":"System.ApplicationException","StackTrace":" at Axendo.SC.AM.Iamsterdam.Controllers.Api.AgendaApiController.GetResultsInternal(RequestModel requestModel)\r\n at lambda_method(Closure , Object , Object[] )\r\n
etc.

Here is a picture of firebug:

I hope someone can help me.

解决方案

Interesting question. I was a bit surprised that the site would honor AJAX-request in a browser and even in cURL, but not in PhantomJS. In such cases you have to study and replicate request very carefully, because one of little details probably greatly affects the server's response.

Turned out, it was a cookie and form content-type that had to be set accordingly.

var webPage = require('webpage');
var page = webPage.create();

// courtesy of http://stackoverflow.com/a/1714899/2715393
var serialize = function(obj) {
  var str = [];
  for(var p in obj)
    if (obj.hasOwnProperty(p)) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    }
  return str.join("&");
}

var settings = {
    operation: "POST",
    encoding: "utf8",
    headers: {
        "accept-encoding" : "identity", // https://github.com/ariya/phantomjs/issues/10930#issuecomment-81541618
        "x-requested-with" : "XMLHttpRequest",
        "accept-language" : "en;q=0.8,en-US;q=0.6",
        "authority" : "www.iamsterdam.com",
        "accept":"application/json, text/javascript, */*; q=0.01",
        "content-type" : "application/x-www-form-urlencoded; charset=UTF-8",
        "cookie" : "website#lang=nl"        
    },
    data: serialize({
        Genre: '',
        DateFilter: '03112016',
        DayPart: '',
        SearchTerm: '', 
        Neighbourhoud: '',
        DayRange: '',
        ViewMode: 1, 
        LastMinuteTickets : '',
        PageId: '3418a37d-b907-4c80-9d67-9fec68d96568',
        Skip: 0,
        Take: 12
    }) 
};

page.open('https://www.iamsterdam.com/api/AgendaApi/', settings, function(status) {
    console.log(page.content);
    phantom.exit();
});

这篇关于发布 API 问题后尝试从 api 获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆