I have totally no experience in this area. What I am trying to do is to send a post request to access an API which would return a JSON file.
This is the curl process in PHP:
$url = 'https://app.responseiq.com/apis/reports';
$fields = array('token' => $api_key,
'offset' =>$offset,
'limit' =>$limit,
'widget_id'=>$widget_id
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
$result = curl_exec($ch);
return $result;
This is what I have so far
httr::POST (
url = "https://app.responseiq.com/apis/reports",
query = list (
token = "ABCDEF",
offset =10,
limit =100,
widget_id = "TEST"
),
httr::content_type("application/x-www-form-urlencoded"),
httr::accept("application/json, text/javascript, */*; d=0.2"),
httr::verbose()
)
I am not getting my expected output, which should be data in JSON format.
Any assistance will be greatly appreciated. If I got my concept totally wrong, it would be great if someone can direct me in the right direction to learn more about using the httr package.Thank you.
Related
I am getting an error for the following PHP code:
$curl = curl_init("https://api.openai.com/v1/engines/davinci/completions");
$data = array(
'prompt' => 'how many sundays in 2023',
'max_tokens' => 256,
'temperature' => 0.7,
'model' => 'text-davinci-003'
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Authorization: Bearer sk-MY-API-KEY']);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
$result = curl_exec($curl);
curl_close($curl);
$result = json_decode($result);
print $result->choices[0]->text;
I correctly provided the API Key, but getting this error:
Error message: You didn't provide an API key. You need to provide your API key in an Authorization header using Bearer auth (i.e. Authorization: Bearer YOUR_KEY)
All Engines endpoints are deprecated.
This is the correct Completions endpoint:
https://api.openai.com/v1/completions
Working example
If you run php test.php in CMD, the OpenAI API will return the following completion:
string(23) "
This is indeed a test"
test.php
<?php
$ch = curl_init();
$url = 'https://api.openai.com/v1/completions';
$api_key = 'sk-xxxxxxxxxxxxxxxxxxxx';
$post_fields = '{
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
}';
$header = [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($result);
var_dump($response->choices[0]->text);
?>
Contact form 7 loading infinitely after connecting it to our CRM API though the email is being sent and data getting submitted and saved the user feels that the data has not yet been submitted since the loader keeps moving.
Here's my code added in functions.php
add_action('wpcf7_mail_sent', function ($cf7) {
if(isset($_POST['your-name']) && isset($_POST['your-email']) && isset($_POST['mobile_no'])){
$name = $_POST['your-name'];
$email = $_POST['your-email'];
$phone = $_POST['mobile_no'];
$data = array(
"name"=>"$name",
"email"=>"$email",
"mobile"=>"$phone",
"secret_key"=>"{secret_key}",
);
leadToNoPaperFormAPI($data);
}
});
function leadToNoPaperFormAPI($data=array())
{
if(is_array($data) && !empty($data))
{
$data_string = json_encode($data);
$ch = curl_init('{API_URL}');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
$array = json_decode($result);
}
}
Any help will be highly appreciated. :)
Please note the code gets executed and the form gets submitted & email sent, however, the user on the browser doesn't see the success message just sees the loader.
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've a problem with wordpress xml-rpc api. My code gets some data from an xml and posts to a blog. Page title posted well, there is no problem on blog but custom fields are broken.
Code file, xml, blog settings and database tables they are all utf-8 encoded.
function wpPostXMLRPC($title,$body,$rpcurl,$username,$password,$thumbnail,$cfields,$category,$keywords='',$encoding='UTF-8') {
$title = html_entity_decode(htmlentities($title,ENT_NOQUOTES,$encoding));
$body = html_entity_decode(htmlentities($body,ENT_NOQUOTES,$encoding));
$keywords = html_entity_decode(htmlentities($keywords,ENT_NOQUOTES,$encoding));
array_walk($cfields,arr_encoding); // this function does the same thing with above
$content = array(
'title'=>$title,
'description'=>$body,
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'post_type'=>'post',
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => $cfields
);
$params = array(0,$username,$password,$content,true);
$request = xmlrpc_encode_request('metaWeblog.newPost',$params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
curl_setopt($ch, CURLOPT_URL, $rpcurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8" );
$results = curl_exec($ch);
if(curl_errno($ch))
echo '<hr>curl error:'.curl_error($ch)."<hr>";
curl_close($ch);
return $results;}
and this is arr encoding function:
function arr_encoding($cfields){
if(is_array($cfields))
array_walk($cfields, 'arr_encoding');
else if(is_string($cfields))
$cfields = html_entity_decode(htmlentities($cfields,ENT_NOQUOTES,"UTF-8"));}
Do you have any idea?
OK, here it is:
Don't use
xmlrpc_encode_request('blogger.newPost',$params);
and use:
xmlrpc_encode_request('blogger.newPost',$params,
array('encoding'=>'UTF-8','escaping'=>'markup'));
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.