To be clear, I want the page containing my form (page1.php) to check the data entered by the user from my other server page (https://anotherserver.com/checker) and send it to page1.php as true or false. This is my first time using curl. I can't see anything with this code..
here is my form page code page1.php
function dataFn($url, $data = array()) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 3000);
$data = curl_exec($curl);
curl_close($curl);
return json_decode($data, true);
}
$data = dataFn(base64_encode('my base64 key'), ['value1' => $value2, 'value2' => $value2]);
var_dump($data);
die();
if (!$data) {
// some err
} else {
if ($data['status'] == 0) {
// some code
}
}
here is my checker server page
<?php
// validation page
print_r($_POST['value1']);
?>
CodePudding user response:
You didn't make too many errors. It definitely will not with your code.
Two ways to make your post data.
$post = 'key1=value1&key2=value2&key3=value3';
$post = array('key1'=>value1,'key2'=>value2,'key3'=>'value3');
depending on the data you may need to use urlencode()
$post = urlencode($post);
I do a lot of curl these are my standard post options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
curl_setopt($ch, CURLOPT_ENCODING,"");
these are my troubleshooting options
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
You NEED to see the request (out) and response headers (in), so you NEED these these two options.
These two option must come out after fixing the problem.
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
If it's HTTPS you need this one:
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
I always make my own request headers. You can remove that option, it's not mandatory.
$request = array();
$request[] = "Host: www.example.com";
$request[] = "Accept: text/html,application/xhtml xml,application/xml;q=0.9,*/*;q=0.8";
$request[] = "User-Agent: MOT-V9mm/00.62 UP.Browser/6.2.3.4.c.1.123 (GUI) MMP/2.0";
$request[] = "Accept-Language: en-US,en;q=0.5";
$request[] = "Connection: keep-alive";
$request[] = "Cache-Control: no-cache";
$request[] = "Pragma: no-cache";
If you are having trouble making the POST you look at the header you send and receive.
$data = curl_exec($ch);
if (curl_errno($ch)){echo 'Error: ' . curl_error($ch);}
Sometimes I will save the all response curl info as text and sometime echo
$info = rawurldecode(var_export(curl_getinfo($ch),true));
echo rawurldecode(var_export(curl_getinfo($ch),true));
You want the CURLINFO_HEADER_OUT
from the curl_getinfo()
Then you can see what you really sent.
echo curl_getinfo($ch,CURLINFO_HEADER_OUT);
The http response id very important to know.
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
There is a lot of info in the curl_getinfo which may give you other hints.
If I had the URL I'm sure I could get it working.