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);
Related
I'm wondering if/how you can add custom headers to a cURL HTTP request in PHP. I'm trying to emulate how iTunes grabs artwork and it uses these non-standard headers:
X-Apple-Tz: 0
X-Apple-Store-Front: 143444,12
How could I add these headers to a request?
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-Apple-Tz: 0',
'X-Apple-Store-Front: 143444,12'
]);
https://www.php.net/manual/en/function.curl-setopt.php
Use the following Syntax
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'X-Apple-Tz: 0',
'X-Apple-Store-Front: 143444,12',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding: gzip, deflate',
'Accept-Language: en-US,en;q=0.5',
'Cache-Control: no-cache',
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'Host: www.example.com',
'Referer: http://www.example.com/index.php', //Your referrer address
'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0',
'X-MicrosoftAjax: Delta=true'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
$subscription_key = '';
$host = '';
$request_headers = [
'X-Mashape-Key:' . $subscription_key,
'X-Mashape-Host:' . $host
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$season_data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
exit();
}
// Show me the result
curl_close($ch);
$json = json_decode($season_data, true);
Here is one basic function:
/**
*
* #param string $url
* #param string|array $post_fields
* #param array $headers
* #return type
*/
function cUrlGetData($url, $post_fields = null, $headers = null) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
if (!empty($post_fields)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
if (!empty($headers))
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $data;
}
Usage example:
$url = "http://www.myurl.com";
$post_fields = 'postvars=val1&postvars2=val2';
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$dat = cUrlGetData($url, $post_fields, $headers);
I want to get file contents (without file_get_contents function) via cUrl but I can't open it with cUrl
My link is here
I tested my hosting with requestb.in and results are here
$header = array();
$header[] = 'Accept-Encoding: gzip';
$header[] = 'Connection: close';
$header[] = 'Host: panel.1n3k.com';
$header[] = 'User-Agent: runscope-radar/2.0';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$response = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($ch);
I tried with agent CURLOPT_USERAGENT with my browser's agent.
CURLOPT_REFERER, CURLOPT_ENCODING, CURLOPT_AUTOREFERER
one by one but no one could help me...
How this can be posible ?
try this one to view your link image contents
<?php
header("Content-Type: image/jpeg");
$url = 'http://panel.1n3k.com/6512bd43d9caa6e02c990b0a82652dca/api/media/4/logo/831710_1464331440.png';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch) ;
echo $res;
?>
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.
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);
I'm wondering if/how you can add custom headers to a cURL HTTP request in PHP. I'm trying to emulate how iTunes grabs artwork and it uses these non-standard headers:
X-Apple-Tz: 0
X-Apple-Store-Front: 143444,12
How could I add these headers to a request?
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-Apple-Tz: 0',
'X-Apple-Store-Front: 143444,12'
]);
https://www.php.net/manual/en/function.curl-setopt.php
Use the following Syntax
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/process.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'X-Apple-Tz: 0',
'X-Apple-Store-Front: 143444,12',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding: gzip, deflate',
'Accept-Language: en-US,en;q=0.5',
'Cache-Control: no-cache',
'Content-Type: application/x-www-form-urlencoded; charset=utf-8',
'Host: www.example.com',
'Referer: http://www.example.com/index.php', //Your referrer address
'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0',
'X-MicrosoftAjax: Delta=true'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
print $server_output ;
$subscription_key = '';
$host = '';
$request_headers = [
'X-Mashape-Key:' . $subscription_key,
'X-Mashape-Host:' . $host
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
$season_data = curl_exec($ch);
if (curl_errno($ch)) {
print "Error: " . curl_error($ch);
exit();
}
// Show me the result
curl_close($ch);
$json = json_decode($season_data, true);
Here is one basic function:
/**
*
* #param string $url
* #param string|array $post_fields
* #param array $headers
* #return type
*/
function cUrlGetData($url, $post_fields = null, $headers = null) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
if (!empty($post_fields)) {
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
}
if (!empty($headers))
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $data;
}
Usage example:
$url = "http://www.myurl.com";
$post_fields = 'postvars=val1&postvars2=val2';
$headers = ['Content-Type: application/x-www-form-urlencoded'];
$dat = cUrlGetData($url, $post_fields, $headers);