简体   繁体   English

php + curl发送带有字段的发布请求

[英]php+curl to send a post request with fields

trying to send post request to api, to get an image back. 尝试将发布请求发送到api,以获取图像。

example url: 示例网址:

https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php?user=1003123&format=png&cloudid=asdasdasd&pass=123123123 

the above url works fine in the browser, 上面的网址在浏览器中可以正常工作,
the api doesnt care if the request is get/post, api不在乎请求是否为get / post,

result of my code is always 'invalid input' . 我的代码的结果始终是“无效输入”

code: 码:

$url='https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php';
$u = rand();
$p = rand();

$fields = array(
    'user'=> urlencode($u),
    'pass'=> urlencode($p),
    'format'=> urlencode('jpg'), 
    'cloudid' => urlencode('test')
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$output = curl_exec($ch);

curl_close($ch);

echo $output;

on a side note: is there a way to debug the request in order to see what is being sent ? 附带说明:有没有一种方法可以调试请求以查看发送的内容?

The URL provided isn't working for POST request. 提供的网址不适用于POST请求。 Here is resulting screenshot (I tried using Advance Rest Client ) 这是生成的屏幕截图(我尝试使用Advance Rest Client

在此处输入图片说明

However Its working perfectly with GET method. 但是,它与GET方法完美配合。 So you can continue using GET request method to generate QR code. 因此,您可以继续使用GET请求方法来生成QR码。

I agree that GET isn't much secure compare to POST method but in your case while requesting from curl user won't get to know about such URL parameters (userid, password). 我同意,与POST方法相比,GET不太安全,但是在您的情况下,如果curl用户发出请求,则不会了解此类URL参数(用户ID,密码)。 Because curl request will be sending from your web server and not from client/user's browser. 因为curl请求将从您的Web服务器发送,而不是从客户端/用户的浏览器发送。

Later you can just output the response image you got from the api. 稍后,您可以仅输出从api获得的响应图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM