Notify Remote URL: How To?

Discussion in 'PHP' started by howidid, Aug 16, 2009.

  1. #1
    I have created a paypal ipn script. Now in this script, at the end when I record that transaction is successful, I need to invoke/ping an external url to notify the affiliate script so it records the affiliate sale.

    This remote URL has query string parameters too:

    It's a URL like:
    somedomain.com/idevaffiliate/sale.php?profile=1111&idev_saleamt=1&idev_ordernum=1&idev_option_1=name&idev_option_2=email@gmail.com&idev_option_3=2

    I have tried curl, fopen etc etc..but none of the option works. How can I invoke this exact URL including params?

    Any help pls?
     
    howidid, Aug 16, 2009 IP
  2. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #2
    do a simple redirection/refresh to that page when transaction is successfull
     
    crivion, Aug 16, 2009 IP
  3. VishalVasani

    VishalVasani Peon

    Messages:
    560
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello,

    You can create a cron file and check against your database.

    Try it out.
     
    Last edited: Aug 16, 2009
    VishalVasani, Aug 16, 2009 IP
  4. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #4
    Are you sure curl is not working? Did you "urlencode" values before pinging that site? And did you use right request method (POST/GET)?
    
    $data = array(
    	'profile' => '1111' , 
    	'idev_saleamt' => '1' , 
    	'idev_ordernum' => '1' , 
    	'idev_option_1' => 'name' , 
    	'idev_option_2' => 'email@gmail.com' , 
    	'idev_option_3' => '2');
    
    foreach ($data as $key => $val) {
    	$data[$key] = urlencode($val);
    }
    
    // HTTP GET Request
    $URL = "http://somedomain.com/idevaffiliate/sale.php?" . http_build_query($data);
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    
    // HTTP POST Request
    $URL = "http://somedomain.com/idevaffiliate/sale.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    $data = curl_exec($ch);
    curl_close($ch);
    
    //echo $data;
    
    PHP:
     
    Gray Fox, Aug 17, 2009 IP
  5. howidid

    howidid Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This code is also not posting the request to the URL. If curl is not installed, thisis suppose to give some error, right?

    What could be wrong?


     
    howidid, Aug 17, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Be sure to add ini_set('display_errors', 1); error_reporting(E_ALL); at the top of your scripts so that we can see the errors.
     
    premiumscripts, Aug 17, 2009 IP
  7. howidid

    howidid Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    oh my ...it worked...i was not sending one parameter that is required to process api requests..it works wonderful now...thanks so much :)
     
    howidid, Aug 17, 2009 IP
  8. howidid

    howidid Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    One question - is Curl installed by default on unix boxes? Or do we need to check before running this code?
     
    howidid, Aug 17, 2009 IP
  9. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #9
    No, probably not. PHP needs to be compiled with curl support and it can also be disabled by the admin.

    So just do a check if (function_exists('curl_init')) {} - this will tell you if curl is available.
     
    premiumscripts, Aug 17, 2009 IP
  10. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Most hosting I've seen had cURL compiled with PHP (its a PHP specific thing), which would usually mean cURL itself got installed on the box.

    You can check your own by making a php file with <? phpinfo(); ?> in it, and then running it in a browser.

    You may see a configure line such as this:
    Note the bold to see how it was compiled with curl and the curl path.

    Then further down the page you should see something like this:
     
    kblessinggr, Aug 17, 2009 IP
  11. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Even the cheap Zensix shared hosting at 15$/year has cURL support enabled. file_get_contents() and the remote use of fopen() is more likely disabled by administrators then cURL.

    Even GoDaddy shared hosting supports it:
     
    Last edited: Aug 17, 2009
    kblessinggr, Aug 17, 2009 IP
  12. howidid

    howidid Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    ok...so if it not installed, is there an alternative regular php way to do the same thing?

    Though I can make it a requirement in specification that curl is required, but just in case if there's an alternative way, that would be easy on customers..
     
    howidid, Aug 17, 2009 IP
  13. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #13
    If you can't use cURL then you likely can't use file_get_contents(), and if you can't use that then good chance you can't use fopen remotely. And in that case you simply have no way to do the same thing unless another alternate library was installed.

    For simple fetch&store requests I usually try file_get_contents first then fallback on curl, since normally if file_get_contents is disabled then curl is usually enabled in its place. This function was designed as a file_get_contents replacement in the event the function was disabled, all one had to do was add _curl to the function name in the code:

    
    function file_get_contents_curl($url) {
    	$ch = curl_init();
    	
    	curl_setopt($ch, CURLOPT_HEADER, 0);
    	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    	curl_setopt($ch, CURLOPT_URL, $url);
    	
    	$data = curl_exec($ch);
    	curl_close($ch);
    	
    	return $data;
    }
    
    PHP:
     
    kblessinggr, Aug 17, 2009 IP
  14. howidid

    howidid Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Ok, in this code, POST method is working, but not the GET. I checked idev documentation and it says it can accept response using any method. What could be wrong in the GET method?
     
    howidid, Aug 17, 2009 IP
  15. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #15
    You always had this set?

    
    curl_setopt($ch, CURLOPT_POST, 1);
    
    PHP:
     
    kblessinggr, Aug 17, 2009 IP
  16. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #16
    Seems like PHP tags strip some characters
    
    $URL = "http://somedomain.com/idevaffiliate/sale.php" . http_build_query($data);
    
    PHP:
    is actually
    
    $URL = "http://somedomain.com/idevaffiliate/sale.php?" . http_build_query($data);
    
    Code (markup):
    (notice the question mark after sale.php)
     
    Gray Fox, Aug 17, 2009 IP