我很难在curlopt_url中传递我的变量,我尝试了不同的语法,但是到目前为止没有好的结果,所以我希望您能帮助我。
这是我的代码:
<?php
$header = array(
"authorization: Bearer NDAxNTVhZTgtODQ5MC00MDBjLTg1MmUtNTAwMjZiMWVjNzZiODMzMTVmYzUtZTFi",
"cache-control: no-cache",
"postman-token: 7d61200d-8ce7-0fb1-8cee-fdeddc67eea4");
$tab_ligne = file('roomid.txt');
$roomcount = count($tab_ligne);
$count = 0;
while ($count != $roomcount){
$ouvre=file('roomid.txt');
$temp = $ouvre[$count];
echo $temp;
$cu = curl_init();
curl_setopt($cu,CURLOPT_URL, "https://api.ciscospark.com/v1/messages?roomId=" . $temp . "&max=1");
curl_setopt($cu,CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($cu,CURLOPT_ENCODING,"");
curl_setopt($cu,CURLOPT_MAXREDIRS,10);
curl_setopt($cu,CURLOPT_TIMEOUT,30);
curl_setopt($cu,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
curl_setopt($cu,CURLOPT_CUSTOMREQUEST,"GET");
curl_setopt($cu, CURLOPT_POSTFIELDS, "{}");
curl_setopt($cu, CURLOPT_HTTPHEADER, $header);
$resp= curl_exec($cu);
$err = curl_error($cu);
echo $resp;
curl_close($cu);
...
当我使用此代码时,会出现以下错误:
{"message":"Failed to get activity.","errors":[{"description":"Failed to get activity."}],"trackingId":"NA_0c7ff82c-6b2e-4973-84dd-b0213ab4fd3a"}
谢谢你的帮助
最佳答案
您可以尝试以下代码:
class sparkAPIV1 {
//GLOBAL DATA
var $SparkAccessToken = NULL;
var $callBackUrl = NULL;
var $roomId = NULL;
//JSON URLs IN SPARK API
//THESE ARE THE URL LOCATIONS THAT YOU CAN POST OR GET JSON DATA TO AND FROM
var $roomsurl = 'https://api.ciscospark.com/v1/rooms';
var $messagesurl = 'https://api.ciscospark.com/v1/messages';
var $webhookurl = 'https://api.ciscospark.com/v1/webhooks';
var $peopleurl = 'https://api.ciscospark.com/v1/people';
var $membershipsurl = 'https://api.ciscospark.com/v1/memberships';
/**
* createWebHook| Used to create a WebHook within a room
* @return string Returns the UUID of the WebHook that was created
*/
function createWebHook()
{
$data = array(
'name' => 'MyCallback' . substr($roomId,-6),
'targetUrl' => $this->callBackUrl,
'resource' => 'messages',
'event' => 'created',
'filter' => 'roomId=' . $this->roomId
);
$options = array(
'http' => array(
'header' => "Authorization: Bearer $this->SparkAccessToken\r\nContent-type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data),
),
);
$context = stream_context_create($options);
$result = json_decode(file_get_contents($this->webhookurl, false, $context));
return $result->id;
}
/**
* createRoomMessage AUsed to add a message into a room
* @param string $message Used to contain the string to insert into the room as a message
* @return null no returnable value
*/
function createRoomMessage($message)
{
$data = array(
'roomId' => $this->roomId,
'text' => $message,
);
$options = array(
'http' => array(
'header' => "Authorization: Bearer $this->SparkAccessToken\r\nContent-type: application/json\r\n",
'method' => 'POST',
'content' => json_encode($data),
),
);
$context = stream_context_create($options);
$result = json_decode(file_get_contents($this->messagesurl, false, $context));
return;
}
/**
* getRoomMessages Used to retrieve $max number of messages in a room
* @param numeric $max Used to control the number of messages retrieved from the room
* @return array An array of messages within the room
*/
function getRoomMessages($max=10)
{
$data = array(
'roomId' => $this->roomId,
'max' => $max,
);
$options = array(
'http' => array(
'header' => "Authorization: Bearer $this->SparkAccessToken\r\nContent-type: application/json\r\n",
'method' => 'GET'
),
);
$context = stream_context_create($options);
$result = json_decode(file_get_contents($this->messagesurl . '?' . http_build_query($data), false, $context));
foreach ($result as $item)
{
foreach ($item as $key=>$value)
{
$dataSet[] = $value->text;
}
}
return $dataSet;
}
}
//EXAMPLE USAGE
$sparkClass = new sparkAPIV1();
$sparkClass->SparkAccessToken = 'MySparkUserAccessToken'; //YOUR SPARK USER'S ACCESS TOKEN
$sparkClass->callBackUrl = 'MyCallBackURL'; //URL YOU WANT THE JSON CALLBACK DATA TO GO TO
$sparkClass->roomId = 'MySparkRoomId'; //UUID OF SPARK ROOM
echo 'Webhook ID: ' . $sparkClass->createWebHook(); //CREATES A WEBHOOK FOR THE USER, IN THE ROOM SPECIFIED BY THE ROOM ID
$sparkClass->createRoomMessage('HELLO WORLD!'); //ISSUES A JSON POST TO THE ROOM WITH THE TEXT "HELLO WORLD!"
var_dump($sparkClass->getRoomMessages(15)); //RETRIEVES ALL THE ROOM MESSAGES SPECIFIED (10 BY DEFAULT)
?>
调用消息函数
$sparkClass->getRoomMessages($temp);
其中
$temp
是房间id。