I have a proxy container that makes a curl request to an api container.
Below is the curl request for the proxy container:
$postRequest = [
'email' => $_POST['email'],
'password' => $_POST['password']
];
pretty_var_dump($postRequest);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://api:80');
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_request);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $post_request);
$curl_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($curl_response, true);
pretty_var_dump($response);
This is the code for the api file:
<?php
// return api results
header('Content-Type: application/json; charset=utf-8');
include_once('functions.php');
include_once('config.php');
$conn = new mysqli($host, $user, $pass, $mydatabase);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo "Connected to MySQL server successfully!";
}
$email = $_POST['email'];
$signin_password = $_POST['password'];
$select_query = "SELECT user_password, user_id, user_email, user_status FROM user_login WHERE user_email = '{$email}'";
echo $select_query;
$result = $conn->query($select_query);
if (isset($result)) {
$result = $result->fetch_all(MYSQLI_ASSOC);
pretty_var_dump($result);
if (isset($result[0])) {
$result = $result[0];
if (isset($result['user_password']) && $result['user_password']) {
if ($result['user_password']) {
if (isset($result['user_status']) && $result['user_status'] === "1" && isset($result['user_id'])) {
$_SESSION['loggedIn'] = $result['user_status'];
response(['user_id' => $result['user_id'], 'session' => $result['user_status']], 200, 'Sign in successful');
} elseif (isset($result['user_status']) && $result['user_status'] === "2" && isset($result['user_id'])) {
$_SESSION['loggedIn'] = $result['user_status'];
response(['user_id' => $result['user_id'], 'session' => $result['user_status']], 200, 'Sign in successful');
}
} else {
$_SESSION['loggedIn'] = false;
response('N/A', 401, 'Password incorrect');
}
}
} else {
response('N/A', 401, 'Email address not found');
}
}
For some reason the api file isn't reading the POST data from curl and so every time returns null. MYSQLI, curl have both been installed, and if the API file is manually passed correct usernames and passwords it works perfectly.
Any help would be much appreciated!
EDIT
I have updated my code for the curl request however i am still recieving null values back. The api code remains unchanged. Is there something obvious here i am missing?
$email = $_POST['email'];
$password = $_POST['password'];
// $data = http_build_query($postRequest);
$data = '{"email":"'.$email.'", "password":"'.$password.'"}';
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
);
pretty_var_dump($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://127.0.0.1:86');
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
$curl_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($curl_response, true);
pretty_var_dump($response);
UPDATE
I doubt you need these headers. The Accept: is ok but that is not what a Browser uses.
$headers = array(
"Content-Type: application/json",
"Accept: application/json",
);
And the Content-Type is wrong. Content-Type is included in the response header, this is the request header.
And it needs to be a key value. You just have a string.
$headers = array('Accept: ' => 'application/json');
In the email header those need to be fixed.
$data = "email=$email&password=$password";
In the response you can add
header("Content-Type: application/json");
END OF UPDATE
I just got done posting this on another question. Looks like you could use it too.
You have a problem here for sure. I doubt $post_request belongs in both the CURLOPT_POSTFIELDS and CURLOPT_HTTPHEADER.
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_request);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $post_request)
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";
Related
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']);
?>
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.
After sending a response to me comes with a link to the next page with the products, how do I send requests to it while this link is in the response?
function get_data() {
$array = [];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
$headers = array(
"accept: application/json;charset=utf-8",
"Authorization: Bearer $token",
"Content-Type: application/json-patch+json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curl);
curl_close($curl);
$res = json_decode($resp, true);
while(isset($res['meta']['nextHref'])) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $res['meta']['nextHref']);
$headers = array(
"accept: application/json;charset=utf-8",
"Authorization: Bearer $token",
"Content-Type: application/json-patch+json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curl);
curl_close($curl);
$res = json_decode($resp, true);
$array[] = $res;
}
return $array;
}
You do not need the while cycle there. Change your function so that it excepts an input parameter, and make it recursive by itself, like this:
function get_data($url) {
$array = [];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
$headers = array(
"accept: application/json;charset=utf-8",
"Authorization: Bearer $token",
"Content-Type: application/json-patch+json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curl);
curl_close($curl);
$res = json_decode($resp, true);
$array[] = $res;
if (isset($res['meta']['nextHref'])) $array = array_merge($array, get_data($res['meta']['nextHref']));
return $array;
}
I want to make a simple call to an API from PHP. It's working fine directly in CURL but not in PHP 7.3.
My real token was replace by TOKEN
CURL:
curl -X POST -H 'Authorization: Bearer TOKEN' -H "Content-Type: application/json" --data-binary '{"query":"{cameraList{name}}"}' https://cloud.camstreamer.com/api/graphql.php
PHP:
<?php
$url = "https://cloud.camstreamer.com/api/graphql.php";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers["Authorization"] = "Bearer TOKEN";
$headers["Content-Type"] = "application/json";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = '{"query": "{cameraList{name}}"}';
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
exit;
You can use
$info = curl_getinfo($ch);
$error = curl_error($ch);
to see what happened.
The content type was not sent correctly.
I changed this:
$headers = array();
$headers["Authorization"] = 'Bearer TOKEN';
$headers["Content-Type"] = "application/json";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
To this:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer TOKEN'
));
I know that there are many same questions, and I have tries the solution from many of them but still I am unable to figure this out.
I am trying to send a curl post from one server to another like this
$array = array("businessname" => "Illusion Softwares");
# try hitting the Tracking via CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/testCurl.php");
curl_setopt($ch, CURLOPT_POST, count($array));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
This is what I have on the testCurl.php on the server where I am posting
echo $_REQUEST['businessname'];
exit;
When I run the page it keeps on loading and loading and loading with a time out error message at last.
I have enabled curl on both the servers.
What am I missing ??
add this line,
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/testCurl.php");
curl_setopt($ch, CURLOPT_POSTFIELDS, array("businessname" => "Illusion Softwares"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
try this
$array = array("businessname" => "Illusion Softwares");
$headers = array(
"Content-type: text/xml",
"Content-length: " . strlen($array),
"Connection: close",
);
# try hitting the Tracking via CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.mywebsite.com/testCurl.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 4000);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $array);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);
if (curl_errno($ch)) {
curl_error($ch);
return FALSE;
} else {
curl_close($ch);
return TRUE;
This curl options setup will get you the info you need to find out what went wrong
You may need curl_setopt($ch, CURLOPT_FAILONERROR,true);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT,100);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
Then you need to check for error, if no error then look at the Request and Response Headers. Below I get the Response header and the Request Header is in $info.
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
echo $data;
}
else {
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$responseHeader = substr($data,0,$skip);
$data= substr($data,$skip);
$info = curl_getinfo($ch);
$info = var_export($info,true);
}
echo $responseHeader . $info . $data ;
If you get an Error is is likely a problem with the request.
To customize your request here is an example:
$request = array();
$request[] = 'Host: xxxxxxx';
$request[] = 'User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:39.0) Gecko/20100101 Firefox/39.0';
$request[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
$request[] = 'Accept-Language: en-US,en;q=0.5';
$request[] = 'Accept-Encoding: gzip, deflate';
$request[] = 'DNT: 1';
$request[] = 'Cookie: xxxx
$request[] = 'Connection: keep-alive';
$request[] = 'Pragma: no-cache';
Then include:
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
I'm trying to replicate a image upload to a website yet that website don't give an api function for that. I managed to get the request information using Charles Proxy:
Here is my php code:
$post_data = array(
'photo' => '#'.$filename,
'_csrftoken' => '5ebcec201972ab6304a33d418129cd13',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/api/v1/upload/photo/');
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host: example.com'
));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_COOKIEFILE, 'C:/xampp/htdocs/example/cookies.txt');
$response = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
print_r($response);
echo $http;
This returns a response with http code of 500.
You are not posting correctly.
You do not need Charles Proxy
Before you do the upload (chrome,firefox),
right click select Inspect Element
Select the Network tab
Refresh the page
Select Documents (chrome) or HTML (firefox)
Clear the list
Post your upload
Select the upload Request in the list of Requests
In fireFox Select "Edit and Resend" In Chrome Select "View Source"
On the right side it will display Request and Response Headers
You need to make your Request look exactly like that Request Header
You have to watch for Redirects (e.g. 302) and Cookies that are added during the Redirect.
You are going to want to see the Request and Response Headers in case it does not work to see what went wrong.
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
You may want to get your cookies. Create another curl request to get the upload page.
To capture cookies:
do curl request for upload page
get the Response header ($head)
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$head = substr($data,0,$skip);
$e = 0;
while(true){
$s = strpos($head,'Set-Cookie: ',$e);
if (!$s){break;}
$s += 12;
$e = strpos($head,';',$s);
$cookie = substr($head,$s,$e-$s) ;
$s = strpos($cookie,'=');
$key = substr($cookie,0,$s);
$value = substr($cookie,$s);
$cookies[$key] = $value;
}
Format the captured for the upload request:
$cookie = '';
$show = '';
$head = '';
$delim = '';
foreach ($cookies as $k => $v){
$cookie .= "$delim$k$v";
$delim = '; ';
}
You need to add some options to your curl
Create the POST data string
$post = 'key1=value1&key2=value2&key3=value3';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
Create an array to put the Request Header Key Values
Fill in the Request array with exactly what is in the Request header of your upload.
EXAMPLE:
$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";
Add to curl:
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
Set follow to false. If there is a Redirect you can see what is happening. then create another curl request to the redirected location.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
After the upload curl request Request get the Headers:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT,10);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
$data = curl_exec($ch);
if (curl_errno($ch)){
$data .= 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
$head = substr($data,0,$skip);
$data = substr($data,$skip);
$info = curl_getinfo($ch);
$info = var_export($info,true);
}
echo $head;
echo $info;
If it did not work correctly examine the differences in the Request Header in the $info.