Pradeep
Pradeep

Reputation: 267

call multiple url sequentially with php curl

How to call more than one url in curl using php and must be executed sequentially one after the another..

Below is my php code

   $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);

Only my first curl command is executed remaining is not being executed

Upvotes: 0

Views: 3728

Answers (1)

Sahil Gulati
Sahil Gulati

Reputation: 15141

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;

}

Upvotes: 1

Related Questions