0

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

4
  • You can define a function which accepts URL, with this you can prevent rewriting code everytime, or you can use multi curl but make sure it is not sequential Commented May 24, 2017 at 5:41
  • @SahilGulati..my first request is post and remaining all are get... Commented May 24, 2017 at 5:42
  • @Pardeep For that you can pass multiple parameter one as URL and other as Method Commented May 24, 2017 at 5:48
  • @SahilGulati..Can u please share an example so that i can understand more clearly.. Commented May 24, 2017 at 5:49

1 Answer 1

1

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;

}
Sign up to request clarification or add additional context in comments.

2 Comments

@Pradeep Can you tell me the issue you are getting? are getting first $response correctly?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.