通过CurlPHP查询API

通过CurlPHP查询API,第1张

概述我正在查看Parse.com REST API并使用 PHP使用的Curl包装器进行调用. 原始卷曲代码(作品): curl -X GET \ -H "X-Parse-Application-Id: myApplicationID" \ -H "X-Parse-REST-API-Key: myRestAPIKey" \ https://api.parse.com/1/classes/S 我正在查看Parse.com REST API并使用 PHP使用的Curl包装器进行调用.

原始卷曲代码(作品):

curl -X GET \  -H "X-Parse-Application-ID: myApplicationID" \  -H "X-Parse-REST-API-Key: myRestAPIKey" \  https://API.parse.com/1/classes/Steps

PHP代码(有效):

$ch = curl_init('https://API.parse.com/1/classes/Steps');curl_setopt($ch,CURLOPT_httpheader,array('X-Parse-Application-ID: myApplicationID','X-Parse-REST-API-Key: myRestAPIKey','Content-Type: application/Json'));curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);curl_exec($ch);curl_close($ch);

多数民众赞成好,但现在当我尝试添加查询约束时:

原始卷曲代码(作品):

curl -X GET \  -H "X-Parse-Application-ID: myApplicationID" \  -H "X-Parse-REST-API-Key: myRestAPIKey" \  -G \--data-urlencode 'where={"steps":9243}' \https://API.parse.com/1/classes/Steps

唉,我们最终得出了我的问题 – 上述代码的PHP模拟是什么?

PHP代码(不起作用):

$ch = curl_init('https://API.parse.com/1/classes/Steps');$query = urlencode('where={"steps":9243}');curl_setopt($ch,1);curl_setopt($ch,CURLOPT_POSTFIELDS,$query);curl_exec($ch);curl_close($ch);

错误响应:

Object ( [code] => 107 [error] => invalID Json: where%3D%7B%22steps%22%3A9243%7D )
解决方法 您的上一个PHP示例已将请求从GET更改为POST.将参数传递给查询字符串而不是POST正文.尝试:
$query = urlencode('where={"steps":9243}');$ch = curl_init('https://API.parse.com/1/classes/Steps?'.$query);curl_setopt(    $ch,array(        'X-Parse-Application-ID: myApplicationID','Content-Type: application/Json'    ));curl_setopt($ch,1);curl_exec($ch);curl_close($ch);
总结

以上是内存溢出为你收集整理的通过Curl / PHP查询API全部内容,希望文章能够帮你解决通过Curl / PHP查询API所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/1033359.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存