簡體   English   中英

PHP如何在PHP中運行此cURL

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM