I do curl from one controller to another.
$ch = curl_init($url . '?' . http_build_query($get));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$headers[] = 'Accept: application/json';
if ($session->get('access_token'))
$headers[] = 'Authorization:' . $session->get('access_token');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
I dump $request->headers->all() in that destination controller and always get only the accept header. If I comment the accept header, it will still be there but only empty. It never gets authorization.
This code does not seem to be missing anything, so what may be wrong?
Apart from checking if $session->get('access_token') is not null (since you check for it in your code, this might be the culprit), I think your Authorization header should rather be :
$headers[] = 'Authorization: Basic ' . $session->get('access_token');
(see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization)
Related
I'm trying to send push notification in laravel 8 using Curl in following way :
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key='. $serverKey;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $arrayToSend);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//Send the request
$response = curl_exec($ch);
//Close request
if ($response === FALSE) {
die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
return $response;
everything is working fine I'm getting the notification.
Issue here is I want to send it in bulk to around 1 Million users, In laravel 8 can we send it in bulk?
Thank you
R
I need help with my code. What it's suppose to do is a user enter the full URL from twitter into a text field ('https://twitter.com/openbayou/status/1487977976033685506') and when the post is saved, it breaks down the url by using explode and then gets data from a tweet via Twitter API v2.
My code:
$tweet_url_raw = get_field('twitter_url');
$parts = explode('/', $tweet_url_raw);
$url = 'https://api.twitter.com/2/tweets/' . $parts[5] . '?expansions=author_id&tweet.fields=created_at&user.fields=username,verified';
$authorization = 'Authorization: Bearer ' . get_field_object('twitter_bearer_token', 'option')['value'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
$tweet_data = json_encode($result);
$tweet_data2 = json_decode($tweet_data);
The code above does get the data:
{"data":{"text":"SEC Super Bowl!!","created_at":"2022-01-31T02:36:09.000Z","author_id":"1471331","id":"1487977976033685506"},"includes":{"users":[{"name":"OpenBayou","verified":false,"id":"1471331","username":"openbayou"}]}}
The problem I'm having is when I'm trying to get individual data from the output. I'm trying to update a text field called tweet with the text from text. I've tried to use update_field('tweet', $tweet_data2->data, $post_id); and it was blank. When I use update_field('tweet2', $tweet_data2["data"], $post_id); all it saves is {
Any idea what I'm doing wrong?
You are almost there.
Since you omitted the associative parameter for json_decode, it defaults to false so you get an object, not an array. You then need to reference it as such:
$tweet_data2 = json_decode($tweet_data);
echo 'Tweet text: ' . $tweet_data2->data->text;
If you prefer to work with arrays, simply pass true to json_decode:
$tweet_data2 = json_decode($tweet_data, true);
echo 'Tweet text: ' . $tweet_data2['data']['text'];
More info on json_decode can be found at the PHP Manual site:
https://www.php.net/manual/en/function.json-decode.php
I am interacting with a external restful API. I was able to perform GET and POST requests successful but my PUT calls are acting strangely.
When I try to perform the task using Postman everything works as expected.
These are my postman parameters:
URl : https://externalapi.com.br/1/sales/0000-8021-46db-9919-8728fec13556/void?amount=15700
Headers: MerchantKey : mykey
Headers: MerchantId: myid
My "curl code" is like that:
$ch = curl_init('https://externalapi.com.br/1/sales/0000-8021-46db-9919-8728fec13556/void?amount=15700');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'MerchantId: ' . $this->merchant_id, 'MerchantKey: ' . $this->merchant_key ));
$res = null;
try{
$res = curl_exec($ch);
}catch (Exception $e){
return $e->getMessage();
}
return $res;
My curl code outputs this
Length Required
HTTP Error 411. The request must be chunked or have a content length.
I added 'Content-Length: 0' to the header. Only when using cUrl with PUT this was necessary.
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'MerchantId: ' . $this->merchant_id, 'MerchantKey: ' . $this->merchant_key, 'Content-Length: 0' ));
I have done all pages listed in core PHP application using wordpress REST API with CURL but I can't delete that pages with CURL.I got message "{code: "rest_cannot_delete", message: "Sorry, you are not allowed to delete this post.}" every time.
Here is my code.
$url = 'http://localhost/wordpress/wp-json/wp/v2/pages/37';
$postdata = 37; //pageID
function callrestapi_curlDelete($url, $postdata) {
$postdata_json = json_encode($postdata);
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
));
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata_json);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
echo json_decode($resp, TRUE);
}
Or if I tried via this code than I am getting the same error.
$.ajax({
url: 'http://localhost/wordpress/wp-json/wp/v2/pages/15 ',
method: 'DELETE',
crossDomain: true,
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'Authorization', 'Basic ' + Base64.encode('admin:admin#123'));
},
success: function( data, txtStatus, xhr ) {
console.log( data );
console.log( xhr.status );
}
});
There is no need to post any data to delete a page/post. The URL is enough as you have already included the ID you want to delete. See example below:
$postid = 108;
$rest_api_url = "http://www.example.com/wp-json/wp/v2/post/".$postid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_URL, $rest_api_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer '.$token,
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
Notice the variable $token for Authorization which i'm assuming you already know how to generate through basic auth.
This will put the post into trash, if you want to delete permanently then add the following to the end of your URL.
?force=true
add this line to your cURL code
curl_setopt($ch, CURLOPT_USERPWD, "username" . ":" . "password");
don't forget to install and activated basic auth master plugin
I have the below code but for some reason I cannot get the correct HTTP response code as it always comes back as 0. I have tried testing a page that only has "header("HTTP/1.1 404 Not Found");" and run cURL on this page bit still I get 0. When I go direct to the page in the browser, the header code is correct. Any ideas?
$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, Array (
"Content-Type: " . $content_type
));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPGET, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT, RESTClient :: USER_AGENT);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, TRUE);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
curl_setopt($curl, CURLOPT_USERPWD, $auth);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
echo $httpcode;
$result = curl_exec($curl);
curl_close($curl);
Naturally, the return code can't be retrieved until after the request has been performed, which happens with the curl_exec() call. Move your curl_getinfo after that line and it should work.