Complete form with cURL using PHP (POST) - php

I'm trying to make a registration system where users that visit us and register to our website will have an account on a secondary website in order for them to be able to see some opportunities.
So here it goes: The website were I want to POST the Form and Register
My code so far
<?php
// set post fields
$post = [
// this i don't master fell free to approach the subject for future reffrance
'authenticity_token'=>'vhmPffEAIiIbjo36K8CI77+YDQfMPBWEG8ymplsGJ0w=',
'user[email]'=>'test#gmail.com',
'user[first_name]'=>'test',
'user[last_name]'=>'test',
'user[password]'=>'12345678',//smart password (had to be 8char long)
'user[country]'=>'Country(placeholder)',
'user[mc]'=>'1560', //id of the country
'user[lc_input]'=>'1475', //id of the dropdown
'user[lc]'=>'1475',//id of the dropdown
'commit'=>'REGISTER',//value of submit
];
$ch = curl_init('https://auth.aiesec.org/users/sign_in');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
?>
Story:
As a user I want to be able to complete a form on domain A and at the same time have an account on domain B using CURL or even JS and AJAX.
If you have any solution to achieve this please let me know :) You can also have a look on the website.
Thank you!
Late edit
I`ve made this in POSTMAN. So I think I should be able to make the for post that information. If you have other suggestions.
In this case server returns 500 as success and 200 as error (security)
Final Working Code
<?php
// set post fields
$post = [
// this i don`t master fell free to approach the subject for future reffrance
'authenticity_token'=>'NKWAg8mGA9gUueBaJ5Og+oEOC5O2rZarZzMK+GnjuCA=',
'user[email]'=>'',
'user[first_name]'=>'',
'user[last_name]'=>'',
'user[password]'=>'',//smart password (had to be 8char long)
'user[country]'=>'',
'user[mc]'=>'', //id of the country
'user[lc_input]'=>'', //id of the dropdown
'user[lc]'=>'',//id of the dropdown
'commit'=>'REGISTER',//value of submit
];
$ch = curl_init('https://auth.aiesec.org/users/');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
?>

You are connection via https. So you should add these lines:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
I tested your code and the website responded "Wrong username / password". That means curl is connecting correctly.

Related

PHP: cURL or header - faster and better way to send cross domain data and redirect

I create one small API what collect contact form informations from one website and store in database on another website in managment application what I also build.
On website where is contact form is this code:
// collect all fields
$save = array(
'oAuth'=>'{KEY}',
'secret'=>'{SECRET-KEY}',
'category'=>'Category',
'name'=>'Jon Doe',
'email'=>'jon#doe.com',
'phone'=>'123 456 7890',
// ... etc. other fields
);
// made GET request
$fields=array();
foreach($save as $key=>$val){
$fields[]=$key."=".rawurlencode($val);
}
// Set cross domain URL
$url='http://api.mydomain.com/';
// Send informations in database
$cURL = curl_init();
curl_setopt($cURL,CURLOPT_URL, $url.'?'.join("&",$fields));
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($cURL, CURLOPT_TIMEOUT, 2);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Accept: application/json'));
$output=curl_exec($cURL);
curl_close($cURL);
usleep(1000);
// redirect
header("Location: http://somemysite.com/thank-you-page");
session_destroy();
exit;
My question is do to use cURL like now for this or to use header() function to send GET?
I ask because I not use output here and sometimes redirection start before cURL finish request.
What is faster way to send GET info and not lost data?
The redirection will not start before cURL finishes. What may happen is that cURL fails to load you remote page, and you don't handle that error. You should add something like this:
if($output === false || curl_getinfo($cURL, CURLINFO_HTTP_CODE) != 200) {
// Here you handle your error
}
If you are looking for an alternative to load remote webpages, you can set allow_url_fopen to true in your php.ini and then use this simple function instead of cURL:
$output = file_get_contents($url.'?'.join("&",$fields));

php+curl to send a post request with fields

trying to send post request to api, to get an image back.
example url:
https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php?user=1003123&format=png&cloudid=asdasdasd&pass=123123123
the above url works fine in the browser,
the api doesnt care if the request is get/post,
result of my code is always 'invalid input'.
code:
$url='https://providers.cloudsoftphone.com/lib/prettyqr/createQR.php';
$u = rand();
$p = rand();
$fields = array(
'user'=> urlencode($u),
'pass'=> urlencode($p),
'format'=> urlencode('jpg'),
'cloudid' => urlencode('test')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
on a side note: is there a way to debug the request in order to see what is being sent ?
The URL provided isn't working for POST request. Here is resulting screenshot (I tried using Advance Rest Client)
However Its working perfectly with GET method. So you can continue using GET request method to generate QR code.
I agree that GET isn't much secure compare to POST method but in your case while requesting from curl user won't get to know about such URL parameters (userid, password). Because curl request will be sending from your web server and not from client/user's browser.
Later you can just output the response image you got from the api.

login to dropbox with curl ( user login )

Am trying to log in to Dropbox as a user using cURL and PHP.
$ch = curl_init();
$data = array(
't'=>'hxdlvCcN7SKKcfKCvpEO8-s2',
'lhs_type'=>'anywhere',
'login_email'=>'myemail#mail.com',
'password'=>'mypass',
'login_submit'=>1,
'remember_me'=>'on',
'login_submit_dummy'=>'Sign in'
);
// set cURL options and execute
curl_setopt($ch, CURLOPT_URL, "https://www.dropbox.com/login?lhs_type=anywhere");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$request_token_response = curl_exec($ch);
var_dump($request_token_response );
I get 403 page with this text:
It seems you tried to do something we can't verify. Did you log into a different Dropbox account in a different window? Try clicking here to go back to the page you came from, or just go home.
What am I doing wrong?
It is possible, and there's a current class available to make it easy. https://github.com/jakajancar/DropboxUploader/
It's as easy as
require 'DropboxUploader.php';
$uploader = new DropboxUploader('email#address.com', 'password');
The $uploader->loggedin will return if you are logged in or not.
I think you'll find that the 't' value can only be used once. You need to call for a fresh one each time you go to log in.
If you look at the class suggested in the other post, you will see that it does just that.

PHP cURL syntax so my Web-form will execute a post to a local (IP/command)

GREAT comment thread at: https://stackoverflow.com/a/6398220/1679026 gave me hope but I failed to solve my issue. When I use cURL to fetch the www.php.net page, it works so cURL works
I have a network-appliance that lives on a local LAN (http://192.168.1.117) here, or at my client site the devices (n) are on their LAN (173.18.13.xxx). From a Local_Browser_Address I can type "http://192.168.1.117/#diag/term&cmd=signage+sign+ALARM" and the network-appliance responds with a change to the "ALARM" state. <-- That is what I want to automate with one button!
On my web form I have $_POST PHP code to execute when the Alarm_Button is clicked. I want the user on my clients physical site to be able to click the_button on my web_form (www.mySite.com/index.php?option=com_rsform&formId=27) in -their browser- and have cURL code send/Post/execute that (http-ALARM) address/instruction string. Can I do this thing? No UI Required at or after address/instruction is sent.
I tried:
$c = curl_init('http://192.168.1.117/u/r/l');
curl_setopt($c, CURLOPT_HTTPHEADER, array('#diag/term&cmd=signage+sign+ALARM'));
curl_exec($c);
AND
$curl = curl_init();
echo"<br> the curl resource is ch --> $curl";
curl_setopt ($curl, CURLOPT_URL, "http://192.168.1.117");
curl_setopt ($curl, CURLOPT_POST, "/#diag/term&cmd=signage+sign+CELL_IN_ALARM");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
echo"<br> The RESULT (1) is --> $result";
No error message but no useful result.
First: I am afraid I am being stopped by the security restrictions like "no_outside_web_page_doing_junk_locally" kind of rules.
IF I have to create a local page on an internal web server, I can do this on another device on the same/adjacent network. But the problem remains for me, how to express the ip/command instruction in the cURL tools.
I have scoured the StackOverFlow information and it seems like I am close but missing the "glaring truth" somewhere!
Hope my challenge is worthy of your interest.
Thank you.
---UPDATE---
the "Sent" values (461) above and below the one (484) highlighted have respective POST Data of:
{"version":"1.1","method":"unit.baseInfo","id":28,"params":[]}
{"version":"1.1","method":"term.cmd","id":29,"params":["signage sign CELL_IN_ALARM"]}
{"version":"1.1","method":"unit.baseInfo","id":30,"params":[]}
This shows Types application/jason and URLs of -http://192.168.1.117/jasonrpc-(jasonRemoteProcedureCall?)
Does this simplify or complicate my struggle?
-- New Update --
When I run the following code I get the php.net home page and a blank return "...is -->" for the second return...
// From http://www.tuxradar.com/practicalphp/15/10/2
$curl1 = curl_init();
curl_setopt ($curl1, CURLOPT_URL, "http://www.php.net");
curl_setopt($curl1, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl1);
curl_close ($curl1);
print $result;
//====================
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, "http://192.168.1.117");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($curl);
curl_close ($curl);
//print $result;
echo"<br> The RESULT (1) is --> $result";
The first one is a www and the second one is the device on my network. Is this a primitive Network/Web conflict? Really appreciate the brain cycles here!
You are thinking of CURLOPT_POSTFIELDS.
This can be used like
$ch = curl_init($site);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "field1=value1&field2=value2&field3=value3");
$chx = curl_exec($ch);
It should also be noted that CURLOPT_POST only takes TRUE or FALSE per the documentation found here

Adding events via places return 404 error:

Please excuse my terminology if I get anything wrong, I'm new to Google API
I'm trying to add events via the Places API, for a single venue (listed under the bar category). I've followed the instructions here:
https://developers.google.com/places/documentation/actions?utm_source=welovemapsdevelopers&utm_campaign=places-events-screencast#event_intro
and this is the URL I am posting to (via PHP)
https://maps.googleapis.com/maps/api/place/event/add/format?sensor=false&key=AIzaSyAFd-ivmfNRDanJ40pWsgP1 (key altered)
which returns a 404 error. If I have understood correctly, I have set the sensor to false as I am not mobile, and created an API key in Google apis, with the PLACES service turned on.
Have I missed a vital step here, or would a subsequent error in the POST submission cause a 404 error? I can paste my code in but I thought I'd start with the basics.
Many thanks for your help, advice and time. It's very much apprecciated.
I've added this line to my CurlCall function which I believe should specify a POST, and the result is still the same.
curl_setopt($ch, CURLOPT_POST, 1);
so the whole functions reads
function CurlCall($url,$topost)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADERS, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_VERBOSE, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $topost);
$body = curl_exec($ch);
curl_close($ch);
echo $body;
echo '<br>';
echo 'URL ' . $url;
echo '<br>';
return $body;
}
Are you sure that you are using the POST method and not GET?
You need to specify the format of your request in the URL by changing the word format to either json or xml depending on how you are going to structure and POST your request.
XML:
https://maps.googleapis.com/maps/api/place/event/add/xml?sensor=false&key=your_api_key
JSON:
https://maps.googleapis.com/maps/api/place/event/add/json?sensor=false&key=your_api_key

Categories