简体   繁体   中英

php pb for passing variable in CURLOPT_URL

I have trouble to pass my variable in the CURLOPT_URL, I have try different syntaxes, but with no good result so far, so I hope you can help me.

This is my code:

<?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);
...

When I use this code I get this error:

{"message":"Failed to get activity.","errors":[{"description":"Failed to get activity."}],"trackingId":"NA_0c7ff82c-6b2e-4973-84dd-b0213ab4fd3a"}

Thanks for help

You can try the following code:

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)

?>

and call the message function by

$sparkClass->getRoomMessages($temp);

where $temp is the roomid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM