how to send data server to another server using curl? - php

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.

Related

CURL request in PHP between two containers?

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";

Php CURL Post not working even with curl enabled

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

Trying to replicate web request (image upload) using php and curl

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.

Button makes an HTTP Request and Store the data in the database

I am not familiar with HTTP Request technology, I did some search on the web for example I saw w3schools article about HTTP request but I didn't understand how I will make what I want to do.
To be more specific, I want a button in my website page to do an http request to another site (this one http://localhost/SensorPlatform/index.php) which inside the index.php contain this code/data.
<?php
header('Content-type: application/json');
echo'{"ID":"SPID9999","RSSI":-48,"Time":"","sensors":[{"Type":"AirFlow","Unit":"Analog","Val":0},{"Type":"Temperature","Unit":"C","Val":28.65},{"Type":"SkinConduct","Unit":"microSiemens","Val":-1.00},{"Type":"SkinResist","Unit":"Ohm","Val":-1.00},{"Type":"SkinConductVolt","Unit":"V","Val":0.49},{"Type":"HeartRate","Unit":"BPM","Val":0},{"Type":"02 Saturation","Unit":"%","Val":0},{"Type":"ElectroCardioGram","Unit":"Analog","Val":3.78},{"Type":"BodyPosition","Unit":"^<>_|","Value":3}]}';
?>
What I want to do is that whenever someone is logged in to my site and click this button to check that if this $username has this $spid (ex: SPID9999) from table patient and then get the data from the other site (http://localhost/SensorPlatform/index.php) and store it in my database table which is named as sensors.
Sorry that I don't provide any code but can't find what to do.
Thank you for your time.
Can I assume you can do the verification of the user? I will for now.
The easiest way to make the request is a GET using file_get_contents()
$json= file_get_contents('http://localhost/SensorPlatform/index.php');
$data = json_encode($json,true);
The json_encode($json,true) give you an array of the JSON data.
The other is with curl which is a much more versatile and can do HTTP POST requests. Once you get over the learning curve is simple and reliable.
This is the curl equivalent of the file_get_contents();
$ch = curl_init('http://localhost/SensorPlatform/index.php');
curl_setopt($ch, CURLOPT_ENCODING,"");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_TIMEOUT,100);
curl_setopt($ch, CURLOPT_FAILONERROR,true);
$json = curl_exec($ch);
$data = json_encode($json,true);
The curl setup here is for testing HTTP GET Requests.
$ch = curl_init('http://localhost/SensorPlatform/index.php');
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_USERAGENT,"Mozilla/5.0 (Windows NT 5.1; rv:32.0) Gecko/20100101 Firefox/32.0");
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);
$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));
$responseHeader = substr($data,0,$skip);
$data= substr($data,$skip);
$info = curl_getinfo($ch);
$info = var_export($info,true);
}
echo $responseHeader;
echo $info;
echo $data;
If you need custom Request headers add an array and an additional curl option:
$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";
curl_setopt($ch, CURLOPT_HTTPHEADER, $request);
If you need to do a POST Request:
Prepare the post data and add a couple of curl options:
$post = 'key1=value1&key2=value2&key3=value3';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch,CURLOPT_POST,true);

cURL, script returning 503 Error (service unavailable)

i am trying to login on the page 'http://portal.demo.ascio.com/Logon.aspx' with cURL but i am getting this error "503 Service Unavailable
No server is available to handle this request. ".
I am sending right POST which i get from firebug by login to the normal page.
I have been searching for a while and figured out that maybe i am not sending right header.
Code is:
$url = 'http://portal.demo.ascio.com/Logon.aspx';
$login_string = 'THERE ARE MY POSTS WHICH CONTAINS PASSWORD...thats is definitelly right'
$headers = array();
$headers[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Host: portal.demo.ascio.com";
$headers[] = "Referer: http://portal.demo.ascio.com/Logon.aspx";
$headers[] = "Accept-Encoding: gzip, deflate";
$headers[] = "Accept-Language: en-US,en;q=0.5";
$headers[] = "Content-type: application/x-www-form-urlencoded";
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 5);
curl_setopt($ch, CURLOPT_POSTFIELDS, $login_string);
$result = curl_exec($ch);
print $result;
curl_close($ch);
Thx for help
In general, if you get the error of 500 series, such as 503, it means your request made it to the server side, and something in your input has caused the server to "break", maybe by design of the server itself. Double-check what you are sending and maybe also compare against the service API/Specs.

Categories