[英]PHP How do I run this cURL in PHP
curl -X POST --header 'Content-Type: application/x-www-form-urlencoded' --header 'Accept: text/plain' --header 'apikey: Your API key goes here' -d 'context= "your url encoded contextobj goes here"&message=Hello%20there' 'https://api.gupshup.io/sm/api/bot/demobot/msg'
我尝试使用其他答案,但无法获得输出。
我在这里做了什么:
$msg=urlencode("Hello world");
$headers = ['Content-Type:application/x-www-form-urlencoded','Accept:text/plain','apikey:xxxxxxxxxxxxxxxxxxxxxxxxxxx',];
$context=http_build_query(json_decode('{"botname":"demobot2","channeltype":telegram","contextid":"xxxxxx","contexttype":"p2p"}'));
$ch = curl_init("https://api.gupshup.io/sm/api/bot/botname/msg");
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POST, 1);
$data = 'context='.$context.'&message=.$msg.';
curl_setopt($ch, CURLOPT_POSTFIELDS,$data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result =curl_exec($ch);
echo $result;
这是工具PostMan生成的代码,我对您的CURL进行了注释,然后提出了一个请求,并单击了generate code
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.gupshup.io/sm/api/bot/demobot/msg",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "context=your%20url%20encoded%20contextobj%20goes%20here&message=Hello%20There",
CURLOPT_HTTPHEADER => array(
"accept: text/plain",
"apikey: Your api key goes here",
"cache-control: no-cache",
"content-type: application/x-www-form-urlencoded",
"postman-token: 7d8384ea-359d-f845-bb72-6063a1aba50c"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.