php curl vs python GET request

最后都变了- 提交于 2020-02-06 08:05:58

问题


I have an issue trying to convert a python GET request to php curl. This is what the python command looks like :

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

I'm trying to do the same thing in 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);

This couldn't be any simpler, however I am getting the following error :

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

回答1:


OK here's the fix for anyone interested, the idea is to produce the curl command: curl -GET -d 'token=abcdefg'

This can be achieved in php curl forcing a GET providing post fields...

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


来源:https://stackoverflow.com/questions/58792670/php-curl-vs-python-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!