Web Analytics Made Easy -
StatCounter cURL problems - CodingForum

Announcement

Collapse
No announcement yet.

cURL problems

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • cURL problems

    Basically, there is a secured site, which I can log into and then add data via a form once I'm logged in. I can pass the login information in the URL, and I can get it to work if, for example, I type the address into my browser bar, like so:

    PHP Code:
    https://my.example.com/authentication.php?user=Username&pass=PW&destination=https://someurl.example.com/Something 
    This will bring me to the page I want to post form data to. Which is great, except that if I use this url in my code, it doesn't work. It just brings me back to the initial login page. I should also say that if I use:

    PHP Code:
        header("Location: https://my.example.com/authentication.php?user=Username&pass=PW&destination=https://someurl.example.com/Something"); 
    I will be brought to the form that I want to post data to. The problem is that I don't want to actually redirect to the page, I just want to get to the form that I need to post information to.

    If I attempt to call this URL in my code, it doesn't work. There are multiple redirects that occur if I type it in the address bar, and I've used FOLLOWLOCATION, 1, but that doesn't seem to help.

    The latest iteration of code attempts to request the page that sets the cookies, and then make subsequent requests that include the cookies, but it still winds up kicking me back to the first page.

    PHP Code:
    $cookie_jar tempnam('/tmp','cookie');

    $c curl_init('https://my.example.com/authentication.php?user=Username&pass=PW');
    curl_setopt($cCURLOPT_RETURNTRANSFER1);
    curl_setopt($cCURLOPT_FOLLOWLOCATION1);
    curl_setopt($cCURLOPT_COOKIEJAR$cookie_jar);
    $page curl_exec($c);
    curl_close($c);


    $c curl_init('https://someurl.example.com/Something');
    curl_setopt($cCURLOPT_POST1);
    curl_setopt($cCURLOPT_POSTFIELDS'field=value');
    curl_setopt($cCURLOPT_RETURNTRANSFER1);
    curl_setopt($cCURLOPT_COOKIEFILE$cookie_jar);
    $page curl_exec($c);
    curl_close($c); 
    Can anyone help?

  • #2
    I was able to figure out the first part... I am now able to retrieve the form I want to post to, but now, I can't figure out how to actually *post* to that form....


    Here's the code thus far:

    PHP Code:

    $cookie_jar 
    tempnam('tmp/','cookie');

    $url "https://my.example.com/authentication.php";
    $POSTFIELDS 'username=UNAME&password=PW&destination=https://my.example.com/someotherpage
    $reffer = "https://my.example.com/login/login.htm?destination=https://my.example.com/someotherpage";
    $agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
    $cookie_file_path = "tmp/"; 

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_REFERER, $reffer);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    $result = curl_exec($ch);

    curl_close($ch); 
    Now, I can pass the username and password, and it will redirect me to the form (https://my.example.com/someotherpage), but I can't figure out how to post to that form.

    Any ideas?

    Comment

    Working...
    X