2016-11-19 71 views
0
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' 

我試過使用其他答案,但無法獲得輸出。PHP如何在PHP中運行此cURL

在這裏,我就是我所做的:

$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; 

回答

0

這裏是由工具郵差,我把你的捲曲的評論,然後做了一個請求,並點擊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; 
} 
?> 
+0

嘿感謝生成的代碼!但我沒有得到所需的輸出:/。我的意思是沒有得到消息 –

+0

我沒有gupshup帳戶,所以我不能進一步測試它,但你是否得到任何「錯誤」吐出來?我會推薦使用PostMan工具或任何其他工具(SoapUi等)進行測試,然後以編程方式編寫調用。 – ioneyed

+0

另外,請注意我的編輯 - 如果你做了複製/粘貼,我忘了關閉'?>'標籤。 – ioneyed