doucheng1063 2018-04-11 05:04 采纳率: 100%
浏览 209
已采纳

PHP cURL 400错误无法满足请求

I'm using the bandsintown API for a personal project and my php code results in an error:

400 ERROR The request could not be satisfied. Bad Request

for some queries. I'm not sure why, and would like some help in fixing it. Thanks!

This is my code right now:

$ch=curl_init();

curl_setopt($ch, CURLOPT_URL, "https://rest.bandsintown.com/artists/".$artist."/events?app_id=".$app_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");

$headers=array();
$headers[]="Accept: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

curl_close($ch);

This results in an error on a fair number of artists and I'm not sure why.

Examples of some of the artists where this results in an error are those like BØRNS, MØ (which have normal results through a regular cURL request)

curl -X GET "https://rest.bandsintown.com/artists/B%C3%98RNS/events?app_id=$app_id" -H  "accept: application/json"

which I initially thought might have something to do with the non-standard characters.

But what makes me think this is another issue is that Calvin Harris and other similar artists with standard lettering throw the same error (and they also work fine on a regular cURL request)

curl -X GET "https://rest.bandsintown.com/artists/Calvin%20Harris/events?app_id=$app_id" -H  "accept: application/json"

So, I think there might be something wrong with the way I made my php cURL.

Any help would be much appreciated. Thanks again!

  • 写回答

1条回答 默认 最新

  • dqdt45183 2018-04-11 07:10
    关注

    probably because you don't urlencode $artist and $app_id. for example, a space is supposed to be encoded with + or %20, Ø is %C3%98, and so on. use urlencode(), or if that doesn't work, use rawurlencode().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题