Sending a request to another URL for a PHP script? - php

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);
?>

Related

How would one simulate a post click with curl?

So essentially I am trying to simulate a form post using php curl. I use something similar to below...
curl_setopt($ch, CURLOPT_URL, 'http://example.com/update/93827');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'Subject=test&Content=test');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
Now that's all fine however, the website I am using requires that every field be complete and by what I saw when I was doing it manually and looking on firebug... It looks a tad unreasonable to get all that data (As you can tell from above, I only need to do 2 fields, however the website requires about 17 different fields that aren't required if you do it manually).
I have always gotten away doing it with the above method, but I thought it might just be easier if there was a way to input your data into the fields and actually simulating a post click so the website does what it requires to do.
If you are confused by what I mean I can elaborate more.
Cheer
This is 1 way to do it. It forces a 'refresh' to the url specified after 0 seconds (immediately)
This WILL NOT work if you want to post values to a page, but I note you are using a query string in the url anyway.
echo '<meta http-equiv="refresh" content="0; ,URL=http://http://example.com/update/93827?Subject=test&Content=test">';

cURL not redirecting properly

I've read a lot of cURL questions on here and none of them seem to have had the same problem I am having.
I have a form set up to submit the following:
if(isset($_POST['submitted_form']) && $_POST['submitted_form'] == "yes"){
$ch = curl_init("https://www.pipelinedeals.com/web_lead");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_REFERER, "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."");
curl_setopt($ch, CURLOPT_POSTFIELDS, "developer_mode=".$_POST['developer_mode']."&thank_you_page=".$_POST['thank_you_page']."&w2lid=".$_POST['w2lid']."&assign_to_id=".$_POST['assign_to_id']."&lead[lead_source]=".$_POST['lead[lead_source]']."&lead[full_name]=".$_POST['lead[full_name]']."&lead[phone]=".$_POST['lead[phone]']."&lead[email]=".$_POST['lead[email]']."");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
}
Before anyone asks why I am using such a convoluted method to post the data, rather than just submitting the form, it is because I want to use the data for some other purposes before passing it on to PipelineDeals.
It's so close to working properly I can taste it. My problem is that when I run the code on say "http://exampledomain.com" it goes ahead and posts the data perfectly. The problem is it isn't performing the redirect properly.
What happens is the data posts. "http://exampledomain.com" is left in the address bar, the content of "https://www.pipelinedeals.com/web_lead" loads on the page (and loads in a way that demonstrates it received the posts), then the content of "http://exampledomain.com" loads under it. It's like cURL is pulling in the content of the other page, rather than redirecting the user to it.
Any suggestions?
It's like cURL is pulling in the content of the other page, rather
than redirecting the user to it.
cURL won't actually redirect the user to the page in question. The client's browser is completely oblivious to what cURL is doing. As far as the browser is concerned, it is simply receiving this data back from your server (which is executing the POST request).
To solve this, you might want to setup a hidden form and then submit that form via JavaScript. Small example:
<form id="pipeline" action="https://www.pipelinedeals.com/web_lead" method="post">
<input type="hidden" name="w2lid" value="<?php echo $_POST['w2lid']; ?>">
<!-- the rest of your POST vars -->
</form>
And the JS:
document.getElementById('pipeline').submit();
Because that is exactly what curl does. It grabs the output of your request, and then you can do some fancy things with it if you like, and then you use that information to render your own page for example.
Are you not looking for: header("Location: http://www.example.com/"); /* Redirect browser */
http://php.net/manual/en/function.header.php
(possibly together with the CURL of course)

Need to scrape contents of website that requires an "i agree" cookie to be set

From everything I've read, it seems that this is an impossible. But here is my scenario:
I need to scrape a table's content containing for sale housing information. The page is not password protected or anything, but you first have to click an "I Agree" link on the previous page so that a cookie gets set saying you agree that the content may not be 100% accurate. You are only then shown the data. Is there any way at all to accomplish this using php/jquery/javascript? I know you cannot create an iframe because of the fact that it is cross-domain. I also do not have access to this other website.
Thanks for any answers, as I'm not really expecting anything positive. :) And many thanks if you can tell me how to do this. :D
Use server side script (PHP using cURL) to crawl the website and return the information you need. Make sure you set the appropriate HTTP header with your request that represents the "I agree" cookie.
Sample:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/');
curl_setopt($ch, CURLOPT_COOKIE, 'I_Agree=1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$responseBody = curl_exec($ch);
curl_close($ch);
// Read the information you need from $responseBody and return it as response body
?>
Now you can access the information from your website by calling your server side script above. For details about how to use cURL take a look at the documentation.
CURL can store or recall cookies from a file depending on the options you set. Here is the "cookiejar" example:
http://curl.haxx.se/libcurl/php/examples/cookiejar.html
Check out the CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE options

How do I pass Variables from my site to another site without leaving my site?

I would like to be able to send variables to another website without actually going to the website using php.
I am building an ecommerce website where the shipping warehouse is being outsourced. After the person checks out with their products, I would like to send some variables over to the shipper's website using $_GET['vars']. This is a completely different URL. The problem is, I don't want the person actually going to the shipper's webpage. I just want to ping that info over there.
Is is possible to send Variables via URL to another site without leaving yours?
Yes, you can. the simplest way:
$contents = file_get_contents("http://example.com/some/page.php?var=abcd");
For more advanced features see Curl.
You should be storing all the relevant order info within your database then using cron to trigger a script that will process the unprocessed, this way systematic checks can be made on orders before any request to your outsource site. Dont rely on your users browser to hit a curtain point in the order process to trigger the API call or trust them not to triple click or inject values before submitting.
And I advise to use curl todo your actual request as its faster. Something as simple as:
<?php
function curl_do_api($url){
if (!function_exists('curl_init')){
die('Sorry cURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
?>
actually there is a more simpler way of solving this...using ajax
include the jquery header first
refer here
http://api.jquery.com/jQuery.ajax/
Both are right and you should make your choice based on security you're looking for.
cURL will be more secure and you should use it if you do not want to pass some argument in query string. At the same time when you pass data using file_get_cotents("URL?data=value"); you will have limit of about 2k for data being passed.
On the other side cURL will be secure if you use it with https it's much more secure. With cURL you will also be able to post files and emulate form post.

Redirecting to a different URL using PHP and passing some POST variables (Parameters)

How can i redirect to a different page along with passing some POST parameters using PHP ?
You cannot "redirect" with the POST method per se, what you're after is to execute a POST request to the site you were planning to redirect to. Have a look at the cURL POST example from http://php.net/manual/de/book.curl.php:
$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '#/home/user/test.png');
curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
Substitute the CURLOPT_URL with your target and set the required fields in the $data array. For this to work your PHP needs to have the cURL module enabled.
Alternatively you could store all the data you plan to send in the POST in your session and just have the target read from there.
you need to store the POST parameters in SESSION variable
How can i redirect to a different page along with passing some POST parameters using PHP?
That is difficult to do, because redirecting needs both, the server and the browser.
Your script can tell the browser that it should redirect.
But the browser, according to the specs, must get confirmation to allow to send the POST request to the redirected URL.
But even so, not all browsers will re-send the post data with the redirect:
Note: When automatically redirecting a POST request after
receiving a 301 status code, some existing HTTP/1.0 user agents
will erroneously change it into a GET request. (Ref)
So as this does not consistently work and I guess you don't want to have the user to press a button to perform the redirect, you can't create easily what you're looking for with just a redirect.
However as Kashif Khan wrote, you can store the submitted post data into some session and then redirect the user to a new location in which you could read again from that session.
To have this working in the browser nicely, use the 303 See Other status code for the redirect.

Categories