PHP中的curl_error(资源ID#6)

时间:2016-03-17 15:42:04

标签: php json curl

我得到了一个" curl_error(资源ID#6)"同时从webhook中恢复数据,我不知道如何解决它。作为问题,我无法在RAW机构获得目标[" text"]。遵循代码:

    <?php
    //Get post data from webhook.
    $webhookResponse = json_decode(file_get_contents('php://input'), true); 

    //Retrieving {"id"} from the data object, via the id property.
    $messageId = $webhookResponse["data"]["id"];
    echo "$messageId";

    $curl = curl_init(); //Starting handle.

    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.mysite.com/v1/messages/" . $messageId,
      CURLOPT_CONNECTTIMEOUT => 60,
      CURLOPT_RETURNTRANSFER => 1, 
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_HTTPGET => true, 
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_SSL_VERIFYPEER => false, 
      CURLOPT_SSL_VERIFYHOST => false, 
      CURLOPT_DNS_CACHE_TIMEOUT => 2, 
      CURLOPT_HTTPHEADER => array(
          "authorization: Bearer XEtY2M3Ni00MTNhLWI3NDYtODdjx",
          "cache-control: no-cache",
          "content-type: application/json"
        ),
      ));

      $response = curl_exec($curl);
      echo "curl_error($curl)"; 
      curl_close($curl); //Closing handle

    //Retriving 'text'
    $idText = ["text"];
    echo "$idText";
?>

然后检索RAW体(200 OK):

    Y2lzY29zcGFyazovL3VzL01FU1NBR0UvZTg4
    {
      "id":"E4ODAtZWM1My0xMWU1LTkyMjctNWYxMjZjZW",
      "roomId":"ZWMxNS0xMWU1LWI5YmQtNjE0ODFmYmE4YWU1",
      "text":"Hello World!",
      "personId":"T1BMRS84MTE3NGUzOC04N2MwLTRmYzUtOTNl",
      "personEmail":"myself@mysite.com",
      "created":"2016-03-17T15:21:23.208Z"
    }
    curl_error(Resource id #6)

请帮忙吗?谢谢!

2 个答案:

答案 0 :(得分:1)

这不是一件事:

PlaySound(TEXT("MAINTHEME.wav"), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);

你想得到你的回应(json)并解码它:

PlaySound((wavid), NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);

答案 1 :(得分:0)

这里的问题是由$ data引起的,因为它是一个数组。所以,&#39; json_encode&#39;应用以将$ data转换为JSON:

$data = array("abc"=> $abc, "def"=> $def, "ghi"=> $ghi);

    $curl = curl_init(); //Starting handle.
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.otherwebsite.com/1.0/sessions?action=action",
      CURLOPT_POST => true,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_POSTFIELDS => json_encode($data), //converting $data to JSON.
      CURLOPT_HTTPHEADER => array(
          "accept: application/json",
          "content-type: application/json"
      ),
    ));

    $resp = curl_exec($curl);
    error_log("POST JSON Request Body: ".$resp);
    curl_close($curl); //Closing handle

    ?>