Make a wp_remote_post when not logged in - php

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.

Related

curl request to localhost takes a huge amount of time

I need to make curl request via PHP to the same local virtualhost url. Its really simple. If I make it via Postman or in command line it takes miliseconds. But as I run it in PHP it is still loading without response. I make a log to file after each step so I can say the code stops after Debugger::log(5555555555555).
This is the code:
public function sendRequest(string $url, array $data = null, $method = 'GET')
{
$url = $this->apiUrl . $url;
$ch = curl_init($url);
$json = json_encode($data);
Debugger::log(2222222222222);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
Debugger::log(2222222222222);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
if ($method == 'POST' or $method == 'PUT') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
}
Debugger::log(33333333333);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_PORT , 80);
Debugger::log(44444444444);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $this->apiKey,
//'X-API-Token: v4yWSYY2Od7-iA6Vi3e8fRNWMkvii-eF',
'Accept: application/json',
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
Debugger::log(5555555555555);
$output = curl_exec($ch);
Debugger::log(6666666666666);
if( curl_errno($ch) ) {
Debugger::log(curl_error($ch), Debugger::EXCEPTION);
throw new \Exception( curl_error($ch) );
}
Debugger::log(77777777777777);
$header = curl_getinfo($ch);
Debugger::log(88888888888888);
curl_close($ch);
$return = array("header" => $header, "output" => $output);
return $return;
}

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

cURL Request return weird value

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

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

Bug Subscription instagram API with cURL

Here is my code to subscribe/unsubscribe to an real time of Instagram API.
The unsubscribe works but not the subscribe. Im not really good with cUrl, I just have use this part of code from http://thegregthompson.com/instagram-real-time-api-php/ with some extra customization.
<?php
$url = "https://api.instagram.com/v1/subscriptions/";
$ch = curl_init();
if (isset($_GET['unsubscribe']))
{
echo "unsubscribe";
$url .= "?client_id=" . $config['instagram']['client_id'] . "&client_secret=" .
$config['instagram']['client_secret'] . "&id=" . $_GET['tag'];
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
}
else
{
$attachment = array(
'client_id' => $config['instagram']['client_id'],
'client_secret' => $config['instagram']['client_secret'],
'object' => 'tag',
'object_id' => $_GET['tag'],
'aspect' => 'media',
'verify_token' => $config['instagram']['verify_token'],
'callback_url'=> 'http://' . $_SERVER['HTTP_HOST'] . '/callback/endpoint.php'
);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_POST, true);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //to suppress the curl output
$result = curl_exec($ch);
curl_close ($ch);
print_r($result);
?>
$config is correctly included.
I spent so much time to try to fix it but no way.
Can you please help me to make the subscription works ?
Ask me if you need more details.
Thanks a lot
Ok, I fixed it: I think we should set the CURLOPT_POST before CURLOPT_POSTFIELDS
Correct
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
Incorrect
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_POST, true);

Categories