cURL Request return weird value - php

i tried this code to post something to form
$url_server = "http://insta.kingdompanel.xyz/addlikes.php";
$postdata = array(
'url' => $url,
'comment' => $comment);
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 500 );
curl_setopt($ch,CURLOPT_URL,$url_server);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postdata);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Access-Control-Allow-Origin: *','Content-type: application/x-www-form-urlencoded'));
$output = curl_exec($ch);
if ($output == FALSE){
echo "CURL ERROR".curl_error($ch);
}
curl_close($ch);
print_r($output);
but it return something like this
is it blocked?
but i read something that website cannot block cURL request.
or i did something wrong? thank you

Related

php cURL POST content length always show -1

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;

why PHP + curl, HTTP POST request returns null?

i am confused why $response does not return anything ,i am new to curl please help,
$post = [
'AreaCode' => '02',
'PhoneNumber' => '33312733',
'InquiryBy' => 'telephone',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://billing.te.eg/api/Account/Inquiry');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Access-Control-Allow-Origin: *',
));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$httpCode = curl_getinfo($ch , CURLINFO_HTTP_CODE);
$response = curl_exec($ch);
if ($response === false) $response = curl_error($ch);
echo stripslashes($response);
curl_close($ch);
while in postman
Please add following line to define request type (GET/POST/..) and then try:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
Download cacert.pem file keep in root directory and set curl option and try
http://curl.haxx.se/ca/cacert.pem
curl_setopt ($h, CURLOPT_CAINFO, base_path()."/cacert.pem");

Make a wp_remote_post when not logged in

Hi i'm developing a plugin which adds a new field to the order (WooCommerce). The field needs to make an ajax request to a file in my plugin, that file then needs to make a cURL request to another website (or wp_remote_post). But i'm experiencing difficulties when making the request.
I can't get the ordinary cURL to work nor the wp_remote_post function.
Here's a snippet of the cURL in my file which the ajax requests to.
<?php
$shipping_place = array(
'country_code' => $country_code,
'postcode' => $postcode,
'street' => $street,
'number_of_droppoints' => $number_of_droppoints
);
$auth = array(
'Content-Type: application/json',
'Authorization: Basic '. base64_encode('user:password')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $auth);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $shipping_place);
$result = curl_exec($ch);
if(curl_errno($ch)){
$msg = 'Curl error: ' . curl_error($ch);
} else {
$result = json_decode($result['body']);
if ( $result->status == 'error' ) {
echo $result;
}
pred($result);
echo $result->result;
}
curl_close ($ch);
?>
Solved: I had to localize the wp-admin script.
That would use the wp function like this
<?php wp_localize_script( $handle, $name, $data ); ?>
Reference here.

PHP cURL Post request not working

I having issues running the correct cURL request. I am meant to do a Post request to the URL.
The example only runs a command line cURL request
$ curl -i -X POST {URL}
The issue I am running the following code and I am getting '400 Bad Request'
$ch = curl_init();
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_TIMEOUT, 10 );
$output = curl_exec( $ch );
curl_close( $ch );
Can anyone help with sending the request correctly.
You're missing the CURLOPT_POSTFIELDS you wanna get by the request.
As explained here you're gonna need to set those fields:
<?php
//
// A very simple PHP example that sends a HTTP POST to a remote site
//
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/tester.phtml");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// in real life you should use something like:
// curl_setopt($ch, CURLOPT_POSTFIELDS,
// http_build_query(array('postvar1' => 'value1')));
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") { ... } else { ... }
?>
Try this:
POST Function:
function httpPost($url,$params)
{
$postData = '';
//create name value pairs seperated by &
foreach($params as $k => $v)
{
$postData .= $k . '='.$v.'&';
}
rtrim($postData, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, count($postData));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
Calling the Function:
$params = array(
"name" => "Ravishanker Kusuma",
"age" => "32",
"location" => "India"
);
echo httpPost("http://hayageek.com/examples/php/curl-examples/post.php",$params);
See, if that helps. Ref: link
curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $array );
Check the manual
To post, just make use of following curl options and try.
(if your url is - "https") {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); //if required.
curl_setopt($ch, CURLOPT_URL, );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
return curl_exec($ch);
I guess the url you are passing doesn't have post fields. eiter you can make use of CURLOPT_POSTFIELDS and your post params their or else attache it to the url using http_build_query(post params)
http://www.url.com?arg1=val1&arg2=val2
Also after spending 5 hours at same code i just found the solution of my problem
If you are using Array in post Fields you have to json_encode that array to recognize as POST parameters
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($curl);
So This is Working CURL Code WITH POST Request
Give a Rep+ If my

Post Curl issue

<?php
$url = "http://website.com/folder/index.php";
$data = array('id' => 'R98s', 'name' => 'Bob', 'content' => 'Hello');
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
curl_exec($handle);
?>
This works great, only 1 problem though,
id like a way to get the content response from the posted data in a variable, and not show as if its the page.
Try the following:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE ); // return into a variable
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec( $ch ); // run!
curl_close($ch);
And never forget the curl_close($handle); at the end.
I always thought you needed this too
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($handle)

Categories