PHP如何在PHP中运行此cURL

时间:2016-11-19 15:34:16

标签: php json curl

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;

1 个答案:

答案 0 :(得分:0)

以下是PostMan工具生成的代码,我收到了您的CURL评论然后发出了请求并点击了const int Trigger = 9; const int Echo = 8; long durationg, inches; void setup() { pinMode(Trigger, OUTPUT); pinMode(Echo, INPUT); Serial.begin (9600); } void loop() { digitalWrite(Trigger, LOW); delay(2); digitalWrite(Trigger, HIGH); delay(5); digitalWrite(Trigger, LOW); duration = pulseIn(Echo, HIGH); inches = duration / 72 / 2; Serial.print(inches); Serial.print("in ,"); Serial.println(); delay(25); }

generate code