我正在尝试为以下API编写PHP代码:
curl -u ********* -d email=tester@gmail.com \
--data-urlencode msg="First message from domain API" \
--data-urlencode url="http://www.domain.com/welcome" \
https://api.domain.com/1/send
<?php
$url = "https://api.domain.com/1/send ";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_USERPWD, "*********");
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "email=test@outlook.com&msg=hi%url=http://yahoo.com/"); // add POST fields
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
处理上述API我做错了什么?
答案 0 :(得分:0)
试试这个:
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode("email=test@outlook.com&msg=hi&url=http://yahoo.com/"));
可能还有其他错误(我无法轻松测试您的代码),但这应该会有所帮助。请注意,除了添加urlencode
功能外,我还将%
更改为&