I'm trying to make two file talk to each other.
'output_file.php' to send data from domain 'a' to input_file located on domain 'b'.
Data from output file will later be send to crm via api.
I'm stuck as I don't know what am I doing wrong, what should I change in these files?
Here is output_file.php:
<?php
//send cURL
$curl = 'https://domain_name/input.php';
$fields = array(
'name' => urlencode($_POST['name']),
'email' => urlencode($_POST['email']),
'tel' => urlencode($_POST['tel']),
);
//var_dump($fields);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//var_dump($fields_string);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $curl);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
//var_dump($result);
curl_close($ch);*/
?>
Here is input_file.php:
// main data about the person. person_id is added later dynamically - PERSON DATA
$person = array(
'name' => 'name from output_file.php',
'email' => 'email from output_file.php',
'phone' => 'tel from output_file.php'
);
You can use below snippet for this. It should be work.
Ps. Please delete POST functions from your output file, it's uncesseray and useless.
$person = array(
'name' => $_REQUEST['name'],
'email' => $_REQUEST['email'],
'phone' => $_REQUEST['phone'],
);
Best,
As you do use POST to send your data, you will need to capture the POST on the target site. As you do use $_POST variables, you may want to have a look into security, to make sure the data recieved can not harm you:
PHP $_GET security, $_POST security best practice
Your Outfile:
<?php
$curl = 'https://domain_name/input.php';
$fields = array(
'name' => urlencode($_POST['name']),
'email' => urlencode($_POST['email']),
'tel' => urlencode($_POST['tel']),
);
// here you do prepare your POST data
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $curl);
// here you define that your data will be sent via POST
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
// this curlopt ensures the output of your destination is captured
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
?>
Your Input/Destination file:
<?php
// user $_POST to populate your array
$person = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['tel']
);
// see the result
var_dump($person);
?>
Related
I am unable to perform a POST request in php, the following is my code:
$ch = curl_init();
$fields = "=var1?var2?var3";
$url = "http://localhost/Profile.php?";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$fields);
$output = curl_exec($ch);
curl_close($ch);
profile.php:
print_r($_POST);
Nothing seems to be displaying on the page just an empty array, if I do via GET it works. What have I done wrong?
Your $fields string should be in the format "key1=value1&key2=value2...", without a prepended =. So, in your case:
$fields = "key1=var1&key2=var2&key3=var3";
Additionally, the print_r($_POST) command in your code will render what has been POSTed to your page, not by your page.
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname' => urlencode($last_name),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
I need assistance with using the data returned after a http post.
Below is my current post code that posts the data to a specific website
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://test.com';
$fields = array(
'surname' => urlencode($surname),
'first_name' => urlencode($first_name),
'dob' => urlencode($dob),
'email' => urlencode($email),
'home_phone' => urlencode($home_phone),
'mobile_phone' => urlencode($mobile_phone),
'work_phone' => urlencode($work_phone),
'postcode' => urlencode($postcode),
'leadid' => urlencode(123),
'affid' => urlencode(123),
'subid' => urlencode(123),
'bank_acno' => urlencode($bank_acno),
'bank_sort' => urlencode($bank_sort),
'amount_required' => urlencode($amount_required),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
?>
I get either one of 2 response's - {result:true} or {result:false}
I need to know is how to do the following :
When I get a {result:true} I want to redirect to url "http://thisurl.com"
When I get a {result:false} I want to return a response "Denied"
Please note the response is not json , it is just returned in that format.
You need to check what $result variable contains and then make the redirect or show denied message according to situation:
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'https://test.com';
$fields = array(
'surname' => urlencode($surname),
'first_name' => urlencode($first_name),
'dob' => urlencode($dob),
'email' => urlencode($email),
'home_phone' => urlencode($home_phone),
'mobile_phone' => urlencode($mobile_phone),
'work_phone' => urlencode($work_phone),
'postcode' => urlencode($postcode),
'leadid' => urlencode(123),
'affid' => urlencode(123),
'subid' => urlencode(123),
'bank_acno' => urlencode($bank_acno),
'bank_sort' => urlencode($bank_sort),
'amount_required' => urlencode($amount_required),
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$result_true = 'true';
$check_true = strpos($result, $result_true);
if ($check_true !== false) {
header('Location: http://url.com');
die;
}
$result_false = 'false';
$check_false = strpos($result, $result_false);
if ($check_false !== false) {
echo "Denied";
}
And in your code you've used:
curl_setopt($ch,CURLOPT_POST, count($fields));
But in php.net manual:
CURLOPT_POST: TRUE to do a regular HTTP POST. This POST is the normal
application/x-www-form-urlencoded kind, most commonly used by HTML
forms.
So counting fields is not the proper way to do this it could be TRUE or FALSE not the count. Of course if $fields array is empty result will be 0 which is equal to FALSE or if it's 1 then it's 1 (TRUE) but when the result of the count is greater than 1 that's makes no sense.
How would I write the following Curl in PHP?
I need to automate this process in php.
$ curl -F file=#/Users/alunny/index.html -u andrew.lunny#nitobi.com -F 'data={"title":"API V1 App","package":"com.alunny.apiv1","version":"0.1.0","create_method":"file"}' https://build.phonegap.com/api/v1/apps
Here is the link to the Phonegap Build API.
http://docs.build.phonegap.com/en_US/developer_api_write.md.html#_post_https_build_phonegap_com_api_v1_apps
Any help would be greatly appreciated.
This is what I have tried so far...
<?php
$url = 'https://build.phonegap.com/api/v1/apps';
$file = 'mobilecontainer.zip';
$fields = array(
'title' => 'Test App',
'create_method' => 'file',
'private' => 'false'
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch,CURLOPT_SAFE_UPLOAD, 'true');
$result = curl_exec($ch);
print_r($result);
curl_close($ch);
You use CURL options improperly.
CURLOPT_SAFE_UPLOAD option disables support for the # prefix for
uploading files in CURLOPT_POSTFIELDS which is exactly what you
need to use.
CURLOPT_POST option expects a boolean value (true or false),
although count($fields) in your case will be evaluated to true
anyway.
-F option in the source curl command forces Content-Type value
to multipart/form-data. This mean that in PHP you have to pass
data to CURLOPT_POSTFIELDS as array. This array should contain two
elements: 'data' - json-encoded data, and 'file' - link to file
to upload.
So the code should look like this:
$url = 'https://build.phonegap.com/api/v1/apps';
$data = array(
'title' => 'Test App',
'package' => 'com.alunny.apiv1',
'create_method' => 'file',
'version' => '0.1.0',
);
$post = array(
'data' => json_encode($data),
'file' => '#mobilecontainer.zip',
);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
So im currently programming a register form, which passes variables with the entered informations to another php file through POST. Well, im performing some checks in this PHP file and whould like to pass the variables after that to the next register form on another php site.
Is this somehow possible ?
I will be thankfull for every answer i get!
-english is not my main language.
You can send informtaion using cURL
Code example from http://davidwalsh.name/curl-post
//extract data from the post
extract($_POST);
//set POST variables
$url = 'http://domain.com/get-post.php';
$fields = array(
'lname' => urlencode($last_name),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
I am wondering how do I make this code support arrays? At the moment the images array only seems to send the first value.
Here is my code:
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'http://api.example.com/api';
$fields = array(
'username' => "annonymous",
'api_key' => urlencode("1234"),
'images[]' => urlencode(base64_encode('image1')),
'images[]' => urlencode(base64_encode('image2'))
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
echo $result;
//close connection
curl_close($ch);
?>
and this is what is received at the api
VAR: username = annonymous
VAR: api_key = 1234
VAR: images = Array
array(3) {
["username"]=> string(10) "annonymous"
["api_key"]=> string(4) "1234"
["images"]=> array(1) { // this should contain 2 strings :( what is happening?
[0]=> string(8) "aW1hZ2Uy"
}
}
What is happening to the second value in images[]?
You are just creating your array incorrectly. You could use http_build_query:
$fields = array(
'username' => "annonymous",
'api_key' => urlencode("1234"),
'images' => array(
urlencode(base64_encode('image1')),
urlencode(base64_encode('image2'))
)
);
$fields_string = http_build_query($fields);
So, the entire code that you could use would be:
<?php
//extract data from the post
extract($_POST);
//set POST variables
$url = 'http://api.example.com/api';
$fields = array(
'username' => "annonymous",
'api_key' => urlencode("1234"),
'images' => array(
urlencode(base64_encode('image1')),
urlencode(base64_encode('image2'))
)
);
//url-ify the data for the POST
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
echo $result;
//close connection
curl_close($ch);
?>
$ch = curl_init();
$data = array(
'client_id' => 'xx',
'client_secret' => 'xx',
'redirect_uri' => $x,
'grant_type' => 'xxx',
'code' => $xx,
);
$data = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_URL, "https://example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$output = curl_exec($ch);