Following is my code which I am using:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->paypal_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: ' . $url_parsed['host']));
$res = curl_exec($ch);
curl_close($ch);
this->paypal_url is the sandbox api url and url_parsed['host'] is the sandbox paypal host name these both are correct, but the problem I am having is on every call $res is always empty.
Before getting this code from paypal developer site I was using following code:
$fp = fsockopen('ssl://' . $url_parsed['host'],"443",$err_num,$err_str,30);
fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n");
fputs($fp, "Host: $url_parsed[host]\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-length: ".strlen($post_string)."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $post_string . "\r\n\r\n");
and failed to open socket error occurred.
Before using ssl I was using this line of code: $fp = fsockopen($url_parsed['host'],"80",$err_num,$err_str,30);
I was getting this response:
Server:
HTTP/1.0 302 Moved Temporarily
Location: https://www.sandbox.paypal.com
Server: BigIP
Content-Length: 0
X-Cache: MISS from proxyco.confiz.com
X-Cache-Lookup: MISS from proxyco.confiz.com:3128
Via: 1.0 proxyco.confiz.com:3128 (squid/2.6.STABLE21)
Connection: close
Please help me finding the solution for this.
Hope following links will help you..
https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_admin_IPNIntro
To download sample code
https://www.x.com/developers/PayPal/documentation-tools/code-sample/216623
IPN Listner is very useful and easy to integrate