php cURL POST content length always show -1 - php

I am new to using cURL for POST request to the server, but the server always show the content-length is -1, my code is below:
$data = array(
'data' => 'Testing data',
'name' => 'Testing',
'no' => '1234'
);
foreach($data as $key=>$value) { $data_string .= $key.'='.$value.'&'; }
$data_string = trim($data_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, Yii::$app->request->post('url'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Length: ' . strlen($data_string)]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
curl_close($ch);
return $result;
Why it's content-length always show -1, thanks~

Updated
Please run this updated code and post the results:
$postData = array(
'data' => Yii::$app->request->post('data'),
'mac' => Yii::$app->request->post('mac'),
'ksn' => '1'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0');
curl_setopt($ch,CURLOPT_URL, Yii::$app->request->post('url'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); //Add this line so we can grab the headers that were sent with your request.
curl_setopt($ch, CURLOPT_FAILONERROR, 1); //Helps with http errors.
curl_setopt($ch, CURLOPT_VERBOSE, 1); //This shows the response headers in the response.
curl_setopt($ch, CURLOPT_HEADER, 1); //Oppps
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
if(curl_exec($ch) === false){
echo 'There was an error with your request.<br>';
echo 'Here are the curl errors:<br>';
echo curl_error($ch) . '<br><br>';
}else{
echo 'There were no errors with the request.<br>';
$result = curl_exec($ch);
echo $result;
}
//Get your sent headers:
$info = curl_getinfo($ch);
echo '<pre>';
echo 'Here are the headers that you sent:<br>';
print_r($info);
echo '</pre>';
curl_close($ch);
return $result;

This has already been suggested in the comments. I've added an a Content type (change it as you wish if your content type is different).
$data = array(
'data' => 'Testing data',
'name' => 'Testing',
'no' => '1234');
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, Yii::$app->request->post('url'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . mb_strlen($data_string) )
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$result = curl_exec($ch);
curl_close($ch);
return $result;

Related

Using Curl with PHP Command line to PHP

This works from the command line how do I convert this to PHP I have tried anything but nothing seems to work.
curl -H "Authorization: Bearer APIKEY" https://www.gocanvas.com/apiv2/forms.xml -F 'username=XXXXXXX#XXXXXXXXXX.com'
Here is a function I have tried.
function getgocan(){
$url = 'https://www.gocanvas.com/apiv2/forms.xml?username=XXXXX#XXXXXXXXXX.com';
$ch = curl_init();
$jsonData = array(
'text' => ''
);
$jsonDataEncoded = json_encode($jsonData, true);
$header = array();
$header[] = "APIKEY";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
}
-F sends form fields in the POST data, not URL query parameters. So you need to put username=XXX into CURLOPT_POSTFIELDS, and it shouldn't be JSON.
You can give an associative array to CURLOPT_POSTFIELDS, and it will encode it automatically.
function getgocan(){
$url = 'https://www.gocanvas.com/apiv2/forms.xml';
$ch = curl_init();
$params = array(
'username' => 'username=XXXXX#XXXXXXXXXX.com'
);
$header = array(
'Authorization: Bearer APIKEY'
);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
curl_close($ch);
var_dump($$result);
}

Why does curl_exec not output data?

header('Content-type: text/html; charset=utf-8');
require 'phpQuery/phpQuery.php';
function debug($arr) {
echo '<pre>'. print_r($arr, true). '</pre>';
}
function getContent($url, $data = []) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__ . '/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__ . '/cookie.txt');
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
$urlAuth = 'https://auth.mail.ru/cgi-bin/auth';
$url = 'https://wf.mail.ru/officers';
$auth_data = [
'Page' => 'https://wf.mail.ru/',
'FakeAuthPage' => 'https://wf.mail.ru/auth',
'Login' => 'MyNail',
'Password' => 'MyPass',
'do_login' => ''
];
$data = getContent($urlAuth, $auth_data);
$data = getContent($url);
debug($data);
In the cookie, the data is written fine, but when I go to the page wf.mail.ru/officers content is not displayed.
Tell me what the error is. Thank.
Added error output, error: Empty reply from server. But why is the answer empty?
I added to getContent()
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 (+http://www.google.com/bot.html)');
if (!empty($data))
curl_setopt($ch, CURLOPT_POST, true);
else
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_FAILONERROR, true);

PHP curl request return false

This is my function:
function postCurl($url, $jsql) {
$data = array('sql' => $jsql);//jsql is a json string
$headers = array('Content-Type: application/x-www-form-urlencoded');
var_dump($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
$output = curl_exec($ch);
return $output;
}
It always returns false. I've tried to make the same request with REST client and it works.
Check in the server whether curl is enabled or not!!
and use the below code
$data = array('name' => $_REQUEST['name']);
$ch = curl_init('www.example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
curl_close($ch);
$xml_output = htmlentities($response);

cURL login into Linkbucks

I'm trying to make a script that log in into my Linkbucks account to get my current stats.
They provide and api, but only for creating links, what I need is you get the statistics.
Things I discovered:
First, you have to stay logged
To get the stats, the website makes a ajax call to: https://www.linkbucks.com/Profile.aspx?task=manageLinks&action=loadPublisherStats with a JSON post like this: {"month":"09/01/2015"} .
With this post its easy to get the information I need, the problem is that my script is not working.
I share the code with you, so please help me.
Any idea or solution, or whatever will be appreciated.
Here is my script:
<?php
$urlLogin = "https://www.linkbucks.com/Default.aspx";
$ch = getSource($urlLogin);
$fuente = curl_exec($ch);
$re = "/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \\/>/";
preg_match($re, $fuente, $matches);
$re = "/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/";
preg_match($re, $fuente, $validation);
$re = "/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/";
preg_match($re, $fuente, $generator);
$post = array(
"ctl00\$ctl00\$phMenu\$LeftMenuBar\$ctl00\$Username" => "yourusername" ,
"ctl00\$ctl00\$phMenu\$LeftMenuBar\$ctl00\$Password" => "yourpassword" ,
"__VIEWSTATE" => $matches[1] ,
"__VIEWSTATEGENERATOR" => $generator[1] ,
"__EVENTVALIDATION" => $validation[1]
);
$data = postData($urlLogin, $post);
echo $data;
function getSource($url, $header = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$config['useragent'] = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33';
curl_setopt($ch, CURLOPT_USERAGENT, $config['useragent']);
curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header));
return $ch;
}
function postData($url , $array) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($array));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx");
$server_output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks');
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$server_output = curl_exec($ch);
return ($server_output);
}
?>
This should work:
$urlLogin = "https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks";
$useragent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33';
$source = getSource($urlLogin, $useragent);
$viewstate = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \/>/", $source);
$generator = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/", $source);
$validation = findStringByRegex("/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/", $source);
$post = array(
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Username' => "yourUsername",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Password' => "yourPassword",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.x' => 0,
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.y' => 0,
"__VIEWSTATE" => $viewstate,
"__VIEWSTATEGENERATOR" => $generator,
"__EVENTVALIDATION" => $validation
);
$data = postData($urlLogin, $post);
echo $data;
function getSource($url, $useragent = null, $header = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header));
return curl_exec($ch);
}
function postData($url, $postArray) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postArray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx");
$server_output = curl_exec($ch);
return $server_output;
}
function findStringByRegex($regex, $source) {
preg_match($regex, $source, $matches);
return $matches[1];
}
Ive cleaned up your code a bit, it was mostly good, you were just missing 2 post fields: ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.x and ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.y'
Finally, thanks to runz0rd I was able to make the code work!!
Thank you so much guys!!
Here is the final version of the code:
<?php
$urlLogin = "https://www.linkbucks.com/Default.aspx?ReturnUrl=%2fManageLinks";
$useragent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.33 (KHTML, like Gecko) Ubuntu/9.10 Chromium/13.0.752.0 Chrome/13.0.752.0 Safari/534.33';
$source = getSource($urlLogin, $useragent);
$viewstate = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"(.*?)\" \/>/", $source);
$generator = findStringByRegex("/<input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"(.*?)\" \\/>/", $source);
$validation = findStringByRegex("/<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=\"(.*?)\" \\/>/", $source);
$post = array(
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Username' => "yourusername",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$Password' => "yourpassword",
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.x' => 0,
'ctl00$ctl00$phMenu$LeftMenuBar$ctl00$LoginImgBtn.y' => 0,
"__VIEWSTATE" => $viewstate,
"__VIEWSTATEGENERATOR" => $generator,
"__EVENTVALIDATION" => $validation
);
$data = postData($urlLogin, $post);
echo $data;
function getSource($url, $useragent = null, $header = null) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_REFERER, (is_null($header) ? 'https://www.google.com/' : $header));
return curl_exec($ch);
}
function postData($url, $postArray) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postArray));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_REFERER, "https://www.linkbucks.com/Default.aspx");
$server_output = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://www.linkbucks.com/Profile.aspx?task=manageLinks&action=loadPublisherStats");
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$fecha = json_encode( array( "month"=> "09/01/2015" ) );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fecha );
$server_output = curl_exec($ch);
return $server_output;
}
function findStringByRegex($regex, $source) {
preg_match($regex, $source, $matches);
return $matches[1];
}
?>

Error in image upload

I want to post some data including a image.I am using curl in php for this.My code is below-
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, 'http://postacity.co.uk:8080/shine/pp/upload/');
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
'creatorid' => '119',
'userfile' => '#/temp/fgf.jpg',
'notice' => 'Image1',
'catid' => '1',
'title' => 'bookofzeus',
'boardid' => '332',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
if(curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
else {
echo $response;
}
}
Every time i am getting the same error
Error: failed creating formpost data
Am i doing any mistake.I have searched for this problem but still no solution found.
The path to the file is the problem: '#/temp/fgf.jpg'
You should take a look at this bug gescription: Bug 50060 - failed creating formpost data if post array value starts with #
The solutions of this problem for example at php freaks.
And use absolute path to the files.
Refer this its working
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"#/path/to/myfile.jpg",
"username"=>"foobar",
"password"=>"secret",
"submit"=>"submit"
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
Also refer this url's
http://joshhighland.com/blog/2010/11/27/using-curl-and-php-to-upload-files-through-a-form-post/
http://blogs.digitss.com/php/curl-php/posting-or-uploading-files-using-curl-with-php/

Categories