如何 POST 到 URL 编码的 API 即 returns 文本?
How to POST to a URL encoded API that returns text?
我的情况很特殊。我们正在使用 Ytel 的 X5 云联络中心。他们有一个 API 允许我们将人员添加到我们的数据库。
您通过 GET 将数据发送到 API(我知道这很奇怪,但我无法控制服务器,因为它归 Ytel 所有)。
所以我 URL 编码了我所有的字段。我知道生成的字符串是正确的,因为如果我复制该字符串并将其粘贴到我的浏览器中并按回车键,我会收到一条成功消息。
成功消息为纯文本。
调用此类 API 的正确方法是什么?
我尝试使用 jQuery/Ajax,但一直使用 "Allow-Control-Access-Origin header is not present, therefore localhost is not allowed..."。所以我尝试将 "datatype" 设置为 "jsonp"。
jQuery.ajax({
url: ytelPostingString,
cache: false,
type: "POST",
dataType: "jsonp",
//What do you want to do when the request succeeds?
success: function(result) {
console.log(result);
console.log("Success");
},
//What do you want to do when the request fails?
error: function(result) {
console.log("Error:" + result);
console.log("Failure");
}
});
这成功了!它成功地将数据放入数据库,唯一的缺点是,服务器的响应不是 jsonp,它是纯文本。所以 "error" 回调将始终被调用,即使 api 请求成功,因为 ajax 期望返回一个 jsonp 对象。
所以现在我正在尝试通过 PHP 进行 API 调用,但我仍然不知道 "correct" 方法是什么?
是否可以 "open" javascript(或 PHP)中的网页并将输出(又名 "webpage")存储到一个变量,我可以然后评价?
另一个有趣的注意事项,如果我在 file_get_contesnts(URL) 之前调用 urlencode(URL) 或 rawurlencode(URL),file_get_contents 会失败。出于某种原因,urlencode 和 rawurlencode 是 encogind : 和 / 导致 file_get_contents 失败。有什么想法吗?
致未来阅读此文的任何人:
我必须 POST 我的值到我服务器上的 php 脚本,并使用 PHP 进行 API 调用。就像调用 file_get_contents($URL);
一样简单
注意:为了让 file_get_contents();
在 URL 上工作,您必须在 PHP.ini 文件中将 "allow_fopen_url" 设置为 "yes"。
- 从不使用
type: "POST"
dataType: "jsonp"
。
- 如果你 运行 在 localhost 上你会得到错误
Allow-Control-Access-Origin header is not present
,所以尝试使用这个插件来允许从源访问:
对于 Google Chrome: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi
对于 Firefox:https://github.com/spenibus/cors-everywhere-firefox-addon
我的情况很特殊。我们正在使用 Ytel 的 X5 云联络中心。他们有一个 API 允许我们将人员添加到我们的数据库。
您通过 GET 将数据发送到 API(我知道这很奇怪,但我无法控制服务器,因为它归 Ytel 所有)。
所以我 URL 编码了我所有的字段。我知道生成的字符串是正确的,因为如果我复制该字符串并将其粘贴到我的浏览器中并按回车键,我会收到一条成功消息。
成功消息为纯文本。
调用此类 API 的正确方法是什么?
我尝试使用 jQuery/Ajax,但一直使用 "Allow-Control-Access-Origin header is not present, therefore localhost is not allowed..."。所以我尝试将 "datatype" 设置为 "jsonp"。
jQuery.ajax({
url: ytelPostingString,
cache: false,
type: "POST",
dataType: "jsonp",
//What do you want to do when the request succeeds?
success: function(result) {
console.log(result);
console.log("Success");
},
//What do you want to do when the request fails?
error: function(result) {
console.log("Error:" + result);
console.log("Failure");
}
});
这成功了!它成功地将数据放入数据库,唯一的缺点是,服务器的响应不是 jsonp,它是纯文本。所以 "error" 回调将始终被调用,即使 api 请求成功,因为 ajax 期望返回一个 jsonp 对象。
所以现在我正在尝试通过 PHP 进行 API 调用,但我仍然不知道 "correct" 方法是什么?
是否可以 "open" javascript(或 PHP)中的网页并将输出(又名 "webpage")存储到一个变量,我可以然后评价?
另一个有趣的注意事项,如果我在 file_get_contesnts(URL) 之前调用 urlencode(URL) 或 rawurlencode(URL),file_get_contents 会失败。出于某种原因,urlencode 和 rawurlencode 是 encogind : 和 / 导致 file_get_contents 失败。有什么想法吗?
致未来阅读此文的任何人:
我必须 POST 我的值到我服务器上的 php 脚本,并使用 PHP 进行 API 调用。就像调用 file_get_contents($URL);
注意:为了让 file_get_contents();
在 URL 上工作,您必须在 PHP.ini 文件中将 "allow_fopen_url" 设置为 "yes"。
- 从不使用
type: "POST"
dataType: "jsonp"
。 - 如果你 运行 在 localhost 上你会得到错误
Allow-Control-Access-Origin header is not present
,所以尝试使用这个插件来允许从源访问:
对于 Google Chrome: https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi
对于 Firefox:https://github.com/spenibus/cors-everywhere-firefox-addon