Curl posting data from local to online fine but how to do opposite

hi

if i run the below curl script on my local xampp server

then it will post data to online website script hosting on online website
http://www.somesite.com/curl_example.php

if curl can post data from localhost to online page then how to do the opposite with curl

means how to post data from online website script to localhost xampp using curl ??

vineet

<?php
$ch = curl_init();    
$url = "http://www.somesite.com/curl_example.php";
datacurl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=value1&var2=value2&var_n=value_n");
postcurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch); // execute 
curl_close ($ch); 
var_dump($output); 
 ?>

If you want to run a script using CURL from a server that’s on the internet, you’ll have to provide a means for it to access your localhost XAMPP PC. So that will involve opening the appropriate ports on your router and providing some static address to the thing that’s running the CURL script.

1 Like

how to open ports on router and get static address with php ??

vineet

You will need to know the IP address and then you will also need to know the open port for the request.

example:

$ch = curl_init();
/* you will need to change the IP to the IP of the computer you plan on sending it to and port number */    
$url = "127.0.0.1:8011;
datacurl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);  
curl_setopt($ch, CURLOPT_POSTFIELDS, "var1=value1&var2=value2&var_n=value_n");
postcurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch); // execute 
curl_close ($ch); 
var_dump($output); 

After that you will need something listening on that port you can build a custom request handler or use a server such as apache to capture the http request.

Alternate is sockets which is also a viable option but ether way something will need to be on the desktop to accept the incoming request. Please make sure you are doing request validation of the http and some sort of authentication because this can be a major security hole.

hi jgetner

the ip which is see on “whatsmyip.org” website is the ip of my computer ??

is that ip address i need ??

how will i get the port number ??

vineet

well the application on your desktop program will determine the port number. If you have something like wamp installed you could use port 80 or change the port in apache that it listens on . If your not going to use a server you will need to write a socket listener. This can be done by creating a socket from your desktop php and the port and the listen for incoming request on that socket. If you google both you should find plenty of tutorials and walkthroughs that can guide you. Google is your friend when programming. Being a good programmer means being an expert at googling.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.