用php curl顺序调用多个URL [英] call multiple url sequentially with php curl

查看:252
本文介绍了用php curl顺序调用多个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用php在curl中调用多个URL,并且必须依次依次执行。

下面是我的php代码

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL,"http://192.168.1.220/cgi-bin/handle_login.tcl");
   curl_setopt($ch, CURLOPT_POST, 1);
   curl_setopt($ch, CURLOPT_POSTFIELDS,
        "user=admin&pw=admin&submit=Login");            
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $response = curl_exec($ch);
   echo "$response";
   curl_close ($ch);


  $ch1= curl_init();
  curl_setopt($ch1, CURLOPT_URL,"http://192.168.1.220/cgi-bin/controller.tcl?sid=$response&type=inverter&inverter=318002N463");
  curl_setopt($ch1, CURLOPT_HTTPGET, TRUE);         
  curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
  $response1= curl_exec($ch1);
  curl_close ($ch1);


  $ch2= curl_init();
  curl_setopt($ch2, CURLOPT_URL,"http://192.168.1.220/cgi-bin/overview.tcl?sid=$response&menuParentId=3");
  curl_setopt($ch2, CURLOPT_HTTPGET, TRUE);         
  curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
  $response2= curl_exec($ch2);
  curl_close($ch2);

只有我的第一个curl命令被执行,其余未执行

推荐答案

对于顺序卷曲请求,您可以定义函数并像这样使用它。如果您不关心顺序请求,则可以使用 multi curl

For sequential curl request you can define function and use it like this. If you are not concerned about sequential request you can use multi curl for that.

<?php

ini_set('display_errors', 1);


$response=curl_request("http://192.168.1.220/cgi-bin/handle_login.tcl","POST","user=admin&pw=admin&submit=Login");
curl_request("http://192.168.1.220/cgi-bin/controller.tcl?sid=$response&type=inverter&inverter=318002N463");
curl_request("http://192.168.1.220/cgi-bin/overview.tcl?sid=$response&menuParentId=3");

function curl_request($url,$method="GET",$postFields="")
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    if($method=="POST")
    {
        curl_setopt($ch, CURLOPT_POST, 1);       
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    }
    else
    {
        curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
    }
    $response = curl_exec($ch);
    echo "$response";
    return $response;

}

这篇关于用php curl顺序调用多个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆