dream989898 2016-05-16 10:08
浏览 77

PHP:如何通过单击链接发送带有REST API调用的cookie?

I'm new to the REST APIs style of web development. I have to send 2 cookies along with 1 REST API. How do I send them together by clicking on a link?

For example, if an user clicks on the link, it is supposed to make the REST API call and send the cookie along with it. I know how to do it in the cURL but I am confused as to how I can trigger that via a link. Ideally, the user should be able to download a zip file by clicking the link. The link should call the REST API and send 2 cookies over to get the zip file. I am able to get the raw code of the zip file using the following code but is not able to achieve that by clicking on the link.

    $getResultsDetails = curl_init();
    $getResults_service_url = '<URL of the REST API call>';
    curl_setopt($getResultsDetails,CURLOPT_URL,$getResults_service_url);
    curl_setopt($getResultsDetails,CURLOPT_COOKIE,"LWSSO_COOKIE_KEY=".$LWSSO_COOKIE_KEY.";QCSession=".$QCSession);
        curl_setopt($getResultsDetails, CURLOPT_RETURNTRANSFER, true); //get response
        $getResults_service_url_response = curl_exec($getResultsDetails);
        curl_setopt($getResultsDetails, CURLOPT_RETURNTRANSFER, true);
        if ($getResults_service_url_response === false) {
            $info = curl_getinfo($getResultsDetails);
            curl_close($getResultsDetails);
            die('error occured during curl exec. Additional info: ' . var_export($info));
        }
        curl_close($getResultsDetails);

Any ideas how I can achieve that?

  • 写回答

1条回答 默认 最新

  • dreamone5156 2016-05-16 10:20
    关注

    This is mostly a duplicate of this but with the added 'how do I?' part. So, heres the bit to add to the answer of that question.

    just use a link to direct your user to the php script making the call.

    you can do this in 2 ways:

    1.

    <a href="yourCurlScript.php">click me</a>
    

    read this answer for details on how to do the cookie part.

    or

    2. use an ajax call using jquery:

    from jquery docs

    $.ajax({
      type: "POST",
      url: "path/to/yourCurlScript.php",
      data: data,
      success: success,
      dataType: dataType
    });
    

    same script is called, so process cookies in the same way.

    for the best user experience I would probably opt for number 2. I dont know what it is that youre planning on retrieving from the API, but you can pipe it into a file using either.

    评论

报告相同问题?

悬赏问题

  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥20 jupyter保存图像功能的实现
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键