PHP - Pass variable to one domain and back again - php

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

Related

How to add Variable and execute to GET Method URL for SMS Gateway

I would like to ask about how to execute and add variable for my SMS gateway, in their API, they want to have us execute their GET method URL with some dynamic variable..
for example I have variables:
$a = 081111111;
$b = message;
and the url should be
https://www.examplegatewaysmsprovider.com/url.php?phone=$a&message=$b
how should I done these things in the same time
Execute the URL so the message can be sent automatically
Add the variable to the URL (what I know is to add . in between the url)
(Additional) I want to execute it on controller in laravel 5.2
thank you for your answers
you can use curl to execute external get request.
$message=urlencode($b);
$url=https://www.examplegatewaysmsprovider.com/url.php?phone=$a&message=$message;
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
curl_close($ch);
Different Way.
step 1. we cant set up transaction table which store all SMS text with contact number also having one flag which maintain sms send or not.
step 2. and in next step we can create schedule(cron job) which execute few records from this table and update flag so sms not send twice.
this the better option which i have.
thanks.

How can I make a request with both GET and POST parameters in PHP with cURL?

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&param2=value2";

Sending a request to another URL for a PHP script?

So I have a contact form on one website, but I want to use the mailing function on another.
I can make forms and get the text and send them in the PHP mail function just fine, I'm just not sure how to do this for another website without opening another tab.
Also, I'm not even sure what to call this.
For example:
I have text fields and a submit button on one website, and I want to send that data to another URL like so:
http://myurl.com?act=phptools&email=example#test.com&subject=Hello&message=How are you?
How would I do this without opening another tab in the browser?
Just set the action attribute of your form to the URL of the processing script on the other domain.
But make sure this is secure, I mean, if you control the second domain, then its okay. Else, you may have to be concerned about sharing data with another domain. But then, I don't know what your requirement is :)
Set the action attribute of the form to the other website url. The user on submit will be taken there, and on success/failure you can redirect the user to the old website url or wherever you want.
Even better use XHR/Ajax, so that the request is made behind the scenes and all you need to do is show the error msg/success msg based on the json response you sent from the other website url. However with this method there are limitations as browsers do not allow cross origin requests. So check if you can enable CORs!
Another thing you can do is send request to current website url only and that would interact with the other website url server-size, hope it makes sense.
As far as i understand your question You can use CURL php functions for this
<?php
$urltopost = "http://somewebsite.com/script.php";
$datatopost = array (
"firstname" => "ex_name",
"lastname" => "ex_lastname",
"email" => "my#email.com",
);`enter code here`
<span id="more-40"></span>`enter code here
$ch = curl_init ($urltopost);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$returndata = curl_exec ($ch);
?>

How do I re-post the entire $_REQUEST to another endpoint?

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.

Array from Requests

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

Categories