php - API 请求在本地机器上工作,而不是在服务器上

标签 php api curl python-requests shopify

我在调用 Shopify API 时遇到了一个有趣的情况。我使用标准程序调用 url 并获取数据,如下所示:

define('SHOPIFY_SHOP', 'myteststore.myshopify.com');
define('SHOPIFY_APP_API_KEY', 'xxxx');
define('SHOPIFY_APP_PASSWORD', 'yyy');
$shop_url = 'https://'.SHOPIFY_APP_API_KEY.':'.SHOPIFY_APP_PASSWORD.'@'.SHOPIFY_SHOP;
$response = Requests::get($shop_url.'/admin/products.json');

我正确地得到了响应,解析了数据,一切都很好。现在,当我将它放到实际服务器(Ubuntu 12.04)时,我注意到来自 Spotify API 的奇怪消息:

[API] Invalid API key or access token (unrecognized login or wrong password)

我尝试创建一个新的应用程序,但还是一样。所以同一个文件和同一个集合在我的机器上工作,但在服务器上不行。 (文件中唯一的区别是请求库的路径,require_once './Requests/library/Requests.php'; 对于 Linux 和 require_once '..\Requests\library\Requests。 php'; for Windows)如前所述,我使用 requests图书馆,我认为必须有一些技巧,图书馆(或其他东西)重写 URl 并且它没有正确进入 Shopify。

我尝试直接将 CURL 与 URL 一起使用,它也能正常工作。谁能告诉我是什么原因造成的?

更新:我转移到另一个解决了这个问题的库,但我想知道是什么导致了这个问题,因为到目前为止我对 Requests 有丰富的经验。

最佳答案

我开始使用同一个库,在发现这个问题后我偶然发现了一些相关的东西: https://github.com/rmccue/Requests/issues/142#issuecomment-147276906

引用相关部分:

This is an intentional part of the API design; in a typical use case, you won't necessarily need data sent along with a request. Building the URL for you is just a convenience.

Requests::get is a helper function designed to make GET requests lightweight in the code, which is why there's no $data parameter there. If you need to send data, use Requests::request instead

$response = Requests::request( 'http://httpbin.org/get', $headers, $data, Requests::GET, $options );
// GET is the default for type, and $options can be blank, so this can be shortened:
$response = Requests::request( 'http://httpbin.org/get', $headers, $data );

关于php - API 请求在本地机器上工作,而不是在服务器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31116820/

相关文章:

javascript - 从 laravel echo 中调用 vue.js 函数时出错

php - 仅替换 PHP 中的特定事件

api - 如何将 Twitter API 用于超过 REST 速率限制的大型 Web 应用程序

jquery - 搜索引擎会读取在 $(document).ready() 或 body load() 中进行的 API 调用吗

python - 是否可以使用 Yahoo 查询语言下载历史财务数据?

c# - 如何将此 HipChat curl 帖子转换为 C#?

php - 移动了服务器,现在出现 fatal error : Call to undefined function mysqli_connect()

javascript - 对象属性在 $.ajax POST 请求中丢失

http - keepalived健康检查无法连接到127.0.0.1

PHPcurl——使用 CURLOPT_USERPWD 变量