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 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真