用php curl顺序调用多个url

时间:2017-05-24 05:39:07

标签: php curl

如何使用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命令,剩余的未执行

1 个答案:

答案 0 :(得分:1)

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

<?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;

}