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?
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:
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?
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.
oh my ...it worked...i was not sending one parameter that is required to process api requests..it works wonderful now...thanks so much
One question - is Curl installed by default on unix boxes? Or do we need to check before running this code?
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.
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:
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:
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..
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:
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?
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)