I'm new to curl. Just I try to post the value using curl script but I'm getting empty response. Help me is there any mistake in my code? How do I post a value using curl
$params = array('name' => 'karthick', 'type' => 'data');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/test.php?action=create');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_POST, true );
// curl_setopt($ch, CURLOPT_USERPWD,$authentication);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
// curl_setopt($ch, CURLOPT_REFERER,'http://www.example.com.au');
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
You can try this code
public function getDataThroughCurlPost($param)
{
$ch = curl_init("$url");
error_reporting(E_ALL);
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, "$param");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
$response = curl_exec($ch);
$ch = curl_close("$url");
return $response;
}
Been using this for quite a few of my websites. Hope this helps.
$sPost .= "<Username>".$username."</Username>";
$sPost .= "<Password>".$password."</Password>";
$url = "YOUR_POST_URL";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$sPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$logindetails = curl_exec($ch);
curl_close($ch);
$xmlarray = xml2array($details);
echo "<pre>";
print_r($xmlarray);
echo "</pre>";
xml2array class found here: http://www.bin-co.com/php/scripts/xml2array/
// curl function starts
function get_web_page( $url )
{
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle compressed
CURLOPT_USERAGENT => "spider", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
// curl function end
$cont = get_web_page("http://website.com/filename.php");
$handle = explode("<br>",$cont['content']);
print_r($handle);
Related
I need to call this command using curl.
http -a $CLIENT_ID:$CLIENT_SECRET --form POST https://api-sandbox.com/auth/token grant_type=client_credentials scope=access_token_only
I have tried following, but not getting through
$URL = "https://api-sandbox.com/auth/token";
$data = array(
'grant_type' => "client_credentials",
'scope' => "access_token_only"
);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_APPEND, "$CLIENT_ID:$CLIENT_SECRET");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$data=curl_exec($ch);
curl_close($ch);
I am getting following error
{"error":"invalid_request","error_description":"Missing form parameter: grant_type"}
How can I do it?
Thx
----------------Additional Information -----------------
oAuth2.0 is being used for authentication
This code should work for you
<?php
$URL = "https://api-sandbox.com/auth/token";
$data = [
'grant_type' => "client_credentials",
'scope' => "access_token_only",
];
$client_id = getenv('CLIENT_ID');
$client_secret = getenv('CLIENT_SECRET');
try {
$ch = curl_init();
// Check if initialization had gone wrong*
if ( $ch === false ) {
throw new RuntimeException( 'failed to initialize' );
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-sandbox.com/auth/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials&scope=access_token_only");
curl_setopt($ch, CURLOPT_USERPWD, $client_id . ':' . $client_secret);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
$content = curl_exec( $ch );
curl_close( $ch );
if ( $content === false ) {
throw new RuntimeException( curl_error( $ch ), curl_errno( $ch ) );
}
/* Process $content here */
// Close curl handle
curl_close( $ch );
} catch ( RuntimeException $e ) {
trigger_error(
sprintf(
'Curl failed with error #%d: %s',
$e->getCode(),
$e->getMessage()
),
E_USER_ERROR
);
}
It worked in a following way:
$content = "grant_type=client_credentials&scope=access_token_only&client_id=".$client_id."&client_secret=".$client_secret;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api-sandbox.com/auth/token');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
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;
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
THis code is not showing up the google home page. Please point out the error in it.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.google.com.kw");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
?>
Remember that if your response is string-based; curl automatically encodes the response in utf8. So it might be necessary to decode the String to get what you want. Otherwise, your code is OK but here is another variant:
<?php
$serviceURL = "https://www.google.com.kw";
$curl = curl_init();
$settings = array(
CURLOPT_URL => $serviceURL,
CURLOPT_POST => false,
CURLOPT_RETURNTRANSFER => true,
);
curl_setopt_array($curl, $settings);
$response = curl_exec($curl);
$response = utf8_decode ( $response ); // <== DECODES THE UTF8 ENCODED STRING.
if(curl_errno($curl)){
var_dump( curl_error($curl) );
exit;
}
print($response);
curl_close($curl);
Try This :
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.google.co.in");
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip,deflate'));
curl_setopt($curl,CURLOPT_ENCODING, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($curl);
print $result;
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.