SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    curl response and redirect

    Hi, I'm posting a form using curl:

    Code:
    <?php
    
    //create the final string to be posted using implode()
    $post_str = implode ('&', $post_items);
    			  
    //Initialize cURL and connect to the remote URL 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://leads..html' );
    
    //Instruct cURL to do a regular HTTP POST
    curl_setopt($ch, CURLOPT_POST, TRUE);
    
    //Specify the data which is to be posted
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
    
    //Tell curl_exec to return the response output as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
    //Follow 302 redirect
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
    
    // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    
    //verify https
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    
    //Execute the cURL session
    $response = curl_exec($ch );
    
    //Close cURL session and file
    curl_close($ch);
    
    echo $response;
    
    $resp = explode("\n\r\n", $response);
    $header = explode("\n", $resp[0]);
    
    echo "<pre>";
    print_r($header);
    echo "</pre>";
    
    ?>
    The response is:

    success: https://www-somesite.html

    or

    Array
    (
    [0] => success: https://www-somesite.html
    )

    My question is how to get the returned URL and redirect the user to it?

    Probably easy but seems to be beyond my very limited php knowledge.

    Thanks.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So you tell cURL to collect a URL, and it is doing that bit fine, you just want to know how to then redirect the user?

    PHP Code:
    <?php
    // do your curl stuff and end up with
    $url "http://somesite.com";

    // do some kind of error check to make sure $url is a URI, then
    header"Location: $url ") ;
    exit;
    The big gotcha with using header() is that your entire php script must output absolutely nothing prior to calling header(), not a white space nor line end, nothing. If you have error_reporting turned on and PHP throws even a non-critical warning then this is output to the browser and you will get a nasty error message instead of a redirect.

  3. #3
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,
    I thought it may be header.
    My main issue tho is how to get the url from the response- success: https://www-somesite.html so i can put into variable?

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Not exactly sure, but try:
    PHP Code:
    $target explode(": "$response);

    $url $target[1]; 
    else show us the full output of :
    PHP Code:
    var_dump$response ); 

  5. #5
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works a treat
    Thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •