【问题标题】:php curl vs python GET requestphp curl vs python GET请求
【发布时间】:2019-11-10 20:31:32
【问题描述】:

我在尝试将 python GET 请求转换为 php curl 时遇到问题。 这就是 python 命令的样子:

requests.get( f'{url}/report', data={'token': reporting_token, 'format': 'pdf'} )

我正在尝试在 php curl 中做同样的事情:

$ch = curl_init();

$params = array(
    'token'=>'abcdefgh',
    'format'=>'pdf',
)

$url = $url.'?'.http_build_query($params);      //this shows http://xxx/report?token=abcdefgh&format=pdf
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);

这再简单不过了,但是我收到以下错误:

{"message":"missing parameter : token"}

【问题讨论】:

  • 查询字符串由? 分隔。您的网址只有在已经有参数时才有效。
  • 抱歉打印错误,我改了,问题出在别处..
  • @Arbiz:data= 不是?token=。你确定不需要x-www-form-urlencoded
  • 我怀疑它与标题有关,我确实尝试过 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));但这没有任何区别。

标签: php python get request


【解决方案1】:

好的,任何有兴趣的人都可以解决这个问题,想法是生成 curl 命令: curl -GET -d 'token=abcdefg'

这可以在 php curl 中强制 GET 提供帖子字段来实现...

curl_setopt($ch, CURLOPT_POSTFIELDS, $get);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
curl_setopt($ch, CURLOPT_URL, $url);

【讨论】:

    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2021-03-29
    • 2018-03-30
    相关资源
    最近更新 更多