douan7601 2012-10-04 18:31
浏览 48
已采纳

在PHP中调用JSON调用的常见做法是什么?

I'm trying to make a JSON call library. It has the following features.

  • be able to customize the header
  • be able to POST any data to server(including form-encoding (a=1&b=2&c=3) and JSON data chunk)
  • parse the response and return as a JSON object

I searched in other questions and found that there are only two answers.

  • use file_get_contents(). This way is pretty simple and easy to use; however, you cannot do anything more than get the content -- you cannot add headers, cannot use POST. But it is usually supported by all servers.
  • use curl. This way seems powerful and you can do nearly everything with it. The disadvantage is that you have to install libcurl and php-curl support on your server, which means you may not use it on servers that have no curl installed.

So, if I want to develop a common library that can be used on most server, is curl a good choice?

Is there any other ways to do this?


In a short word, I'm looking for a PHP version of urllib2 in python - easy to use, powerful, and reliable.

  • 写回答

2条回答 默认 最新

  • dongmoyu0336 2012-10-04 18:45
    关注

    You have two options: curl and HTTP stream context options. Both can accomplish what you have described; curl might not be installed everywhere, while stream contexts are a core feature and are always available.

    Actually, you can also implement your library using sockets, which would be more work but will probably allow you greater control if you need to do weirder things with your requests.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?