Hi I've set up 2 scripts, both communicating back and forth.
One thing I'd like is for one script to post an unknown amount of GETs to the other script.
I'd like the other script to some how be able to grab all the GETS and put the values in an array.
$_GET already is an array. No reason you can't just copy it into your another array whose name is easier to type.
$array = $_GET;
If you simply want to make GET requests from one script to another, put something like
file_get_contents( "http://www.yoursite.com/script_2.php?some_var=some_value" );
And then, in script_2.php just read the variable
$_GET['some_var'];
If I understand you correctly you want to post your some GET data received by script_1.php from script_1.php to script_2.php,right?If so ,this probably can help you
<?php
/*#file script_1.php*/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'url_of_script_2.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_GET);
curl_exec($ch);
?>
And in script_2.php,you will get the GET datas by using $_POST.See curl in manual
Related
Other people have already asked how to do this from perl, java, bash, etc. but I need to do it in PHP, and I don't see any question already asked relating specifically to (or with answers for) PHP.
My code:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
This doesn't work. The destination site has print_r($_GET); print_r($_POST);, so when I examine the $result I should be able to see the fields that are being sent. However, the $_POST array is empty - I only see the get variables. If I remove the ?... query string from the $url, then the POST array is populated correctly. But now I don't have the GET params. How do I do this?
My specific case is, I need to send too much data to fit it in the query string, but I can't send it all as POST because the site I want to submit to is selecting a handler for the posted data based on a variable in the GET string. I can try and have that changed, but ideally I would like to be able to send both get and post data in the same query.
# GET query goes in the URL you're hitting
$ch = curl_init('http://example.com/script.php?query=parameter');
# POST fields go here.
curl_setopt($ch, CURLOPT_POSTFIELDS, array('post' => 'parameter', 'values' => 'go here'));
PHP itself wouldn't decide to ignore the GET parameters if a POST is performed. It'll populate $_GET regardless of what kind of http verb was used to load the page - if there's query parameters in the URL, they'll go into $_GET.
If you're not getting $_POST and $_GET with this, then something is causing a redirect or otherwise killing something. e.g. have you check $_SERVER['REQUEST_METHOD'] to see if your code is actually running as a POST? PHP won't populate $_POST if a post wasn't actually performed. You may have sent a post to the server, but that doesn't mean your code will actually be executed under a POST regime - e.g. a mod_rewrite redirect.
Since you have FOLLOW_REDIRECT turned on, you're simply ASSUMING you're actually getting a post when your code executes.
i don't know maybe you already have but is your $url has the desired get parameters? Like:
$url = "http://example.com/index.php?param1=value1¶m2=value2";
I am in need of passing a variable from one domain to another and then grabbing the results from the second domain and passing it back. I have not done this before so any help would be great.
Basically, the first domain would contain a key. For example:
$key = '1234567890123456';
I would like to send that variable to the second domain which would validate if that key exists in the database and let the first domain know if it does or not. I already have the code for the second domain which would check the key to see if it exists.
However, I am not sure the best way to send the above variable to the second domain and grab the response from the second domain and send it back.
Since this is not a form and should just be triggered once the page is loaded, I don't know the best method to do that.
Any help and guidance on that would be awesome! Thanks!
You can simply send GET data by file_get_contents(). Example:
$result = file_get_contents("http://example.com/script.php?var1=value1&var2=value2");
And here is a very simple code to send POST data to a URL with curl:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://example.com/script.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"var1=value1&var2=value2");
$result = curl_exec($ch);
curl_close($ch);
Source: http://curl.haxx.se/libcurl/php/examples/simplepost.html
I have an application that interacts with an outside API. When the remote serve posts a request containing specific status, I need to re-send the entire contents of that post to a different application through an external URL.
Is it possible to take the entire $_REQUEST and re-post it else where, or do I have to iterate through $_REQUEST and build a new array for posting where I need to?
$_REQUEST is an array. So yes you can post it where you need to. Just use cURL
If I correctly understand your question you're able to do it with CURL:
$ch = curl_init("http://sitename.com/");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "field1=value1&field2=value2");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
where "field1=value1&field2=value2" is replaced with your args
Also you're able to get result of how operation is completed:
after $result = curl_exec($ch); use:
$result_info = curl_getinfo($ch);
now in $result_info['http_code'] is placed HTTP CODE. If it's 200 then SUCCESS (of course the code might be different in some cases - e.g. when it's artificially configured to another code)
you should distinct between $_POST and $_GET values.
$_REQUEST represents all values that are coming by GET and POST, if one key is set in $_POST and $_GET value from $_POST have precedence when getting saved to the $_REQUEST array.
append everything that comes via $_GET to the URL and send everything from $_POST in the request body.
Using $HTTP_RAW_POST_DATA can save the overhead of reencoding, if both segements are a POST method
Lookup CURL, this should be rather easy:
http://davidwalsh.name/execute-http-post-php-curl
I would use the cURL extension.
Hi I am new to php and want to know some alternate function for the header('location:mysit.php');
I am in a scenario that I am sending the request like this:
header('Location: http://localhost/(some external site).php'&?var='test')
something like this but what I wanna do is that I want to send values of variables to the external site but I actually dont want that page to pop out.
I mean variables should be sent to some external site/page but on screen I want to be redirected to my login page. But seemingly I dont know any alternative please guide me. Thx.
You are searching for PHP cUrl:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Set the location header to the place you actually want to redirect the browser to and use something like cURL to make an HTTP request to the remote site.
The way you usually would do that is by sending those parameters by cURL, parse the return values and use them however you need.
By using cURL you can pass POST and GET variables to any URL.
Like so:
$ch = curl_init('http://example.org/?aVariable=theValue');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Now, in $result you have the response from the URL passed to curl_init().
If you need to post data, the code needs a little more:
$ch = curl_init('http://example.org/page_to_post_to.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'variable1=value1&variable2=value2');
$result = curl_exec($ch);
curl_close($ch);
Again, the result from your POST reqeust is saved to $result.
You could connect to another URL in the background in numerous ways. There's cURL ( http://php.net/curl - already mentioned here in previous comments ), there's fopen ( http://php.net/manual/en/function.fopen.php ), there's fsockopen ( http://php.net/manual/en/function.fsockopen.php - little more advanced )
I've got a form say here - http://example.com/palreg.php
Once people register I'll be sending them an email with the link that will allow them to edit their details (I know it's a crazy way of doing things, but I am working on someone else's code so don't mind) for instance the url as such http://example.com/palreg.php?paliD=1234, and when they go to that page the form will be populated with their information so that they can make changes.
Now the problem is that the DB is in a different site and the information has to be passed to that site to perform a select action, to do so I use cURL to send the information like so
$url = "http://example2.com/processXML.php";
$xmlStr will be like this
<table>tab_name</table>
<action>select</action>
<palid>1234</palid>
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, 'xmlstr='.$xmlStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
On the other end (http://example2.com/processXML.php) I convert the xml to an array and perform the query to select pal information based on the id sent.
The real question now is how do I send the retrieved pal information (as xml or json or array) back so that i can populate the form with the returned data.
Also can I do a return $dataArray; in processXML.php and be able to grab it?
OK to make things a bit clearer, what I do in processXML.php is
retrieve the result set and do this
print json_encode($resultArray);
not should I print or return
Thank you, and do let me know if things aren't clear.
just encode it as you want, and echo it to the page. your $data= will contain the echoed contents. Personal preference has been to use JSON since it's so easy to throw around. as in
//Bunch of DB stuff..
$row=mysql_fetch_however_you_handle_it();
echo json_encode($row);
//on your receiving end, that just did the cURL send,
$row=json_decode($data,true);//decode the JSON straight into an array...
When you pull the information from the database and process it in processXML.php, add the results to an array, use json_encode() to convert the array to JSON, and then echo the results to the page.
Just echo out the data you need to send back to the form, in your format of choice, and fetch that response in thepalreg.php.