dongtuo5262 2010-11-16 20:38
浏览 44
已采纳

curl CLI来卷曲PHP

I use the following command in some old scripts:

curl -Lk "https:www.example.com/stuff/api.php?"

I then record the header into a variable and make comparisons and so forth. What I would really like to do is convert the process to PHP. I have enabled curl, openssl, and believe I have everything ready.

What I cannot seem to find is a handy translation to convert that command line syntax to the equivalent commands in PHP.

I suspect something in the order of :

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);

// What goes here so that I just get the Location and nothing else?

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// Get the response and close the channel.
$response = curl_exec($ch);
curl_close($ch); 

The goal being $response = the data from the api “OK=1&ect”

Thank you

  • 写回答

1条回答 默认 最新

  • douba1214 2010-11-16 20:50
    关注

    I'm a little confused by your comment:

    // What goes here so that I just get the Location and nothing else?
    

    Anyway, if you want to obtain the response body from the remote server, use:

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    

    If you want to get the headers in the response (i.e.: what your comment might be referring to):

    curl_setopt($ch, CURLOPT_HEADER, 1);
    

    If your problem is that there is a redirection between the initial call and the response, use:

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误