I have this request in wordpress,
$url = $this->apiUrl . '/' . $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'X-Client: PHP ' . phpversion()
));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, ":" . $this->apiKey);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
switch ($httpVerb) {
case 'post':
curl_setopt($ch, CURLOPT_POST, true);
if ($data) {
$encoded = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded);
}
break;
}
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
echo json_encode(curl_getinfo($ch));
curl_close($ch);
And this is the response
{"url":"https:\/\/api.paylike.io\/cards\/5a05ba1d752c916c2607c5fb","content_type":null,"http_code":400,"header_size":435,"request_size":242,"filetime":-1,"ssl_verify_result":0,"redirect_count":0,"total_time":0.395293,"namelookup_time":0.01238,"connect_time":0.01387,"pretransfer_time":0.031095,"size_upload":0,"size_download":0,"speed_download":0,"speed_upload":0,"download_content_length":-1,"upload_content_length":-1,"starttransfer_time":0.395242,"redirect_time":0,"redirect_url":"","primary_ip":"104.20.8.147","certinfo":[],"primary_port":443,"local_ip":"138.68.167.102","local_port":45414}
The link is ok, but I don't know why do I get an 400 error, try accessing the page and see that is working :
https://api.paylike.io/cards/5a05ba1d752c916c2607c5fb
So where is the error, does wordpress have some limitation ? I tried this code outside of wordpress, and it works. Also I tried spootify my curl, but no results ...
Firstly check if the curl is enabled in your system or server. You can check it by using a simple page with this code.
phpinfo();
If curl is enabled it will show there. If its not enabled enable it.
Next check for the curl errors and clear it. Just add this code after the curl_exec($ch);
'Error Details: ' . curl_error($ch);
It will show you the error.
Related
In a certain API script, I want to run an AJAX file from within a PHP file, to post the vars to the AJAX file. I'd prefer not having to include the AJAX file for a nicer solution, since it's made to receive post vars. When I run the cURL locally using XAMPP I get the following error:
Curl error: Could not resolve host: C
Is semicolon an invalid char for cURL? How can I run cURL locally, pointing towards a file on the same server?
$isLocal = (!empty($_SERVER['HTTP_HOST']) && strpos($_SERVER['HTTP_HOST'], 'localhost') !== false);
if ($isLocal) {
$documentRootWildcard = 'C:/_foretag/TourTask/Internet/Hemsidor/example.com';
} else {
$documentRootWildcard = '/home/user5/_domains/example.com';
}
$curlUrl = $documentRootWildcard . '/ajax/bookings/save.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $curlUrl);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'ea5e7f894cde433a987d9b9979dfed5f:7ff527e78ace4e9291ee6f7a7d52141e');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json')
);
$curlResponse = curl_exec($ch);
if ($curlResponse === false) {
echo 'Curl error: ' . curl_error($ch);
} else { //Successfully sent
}
curl_close($ch);
How can I solve this? Or is there a better solution? Thank you!
I followed the instructions in the official vsphere site to get info from the server and from the answer of another user here .
From what I have understood, firstly I have to get the session id (cis id), but I got "null" as a result.
I tried multiple codes but the result is the same:
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array(
'Content-Type: application/json',
'Accept: application/json',
'vmware-use-header-authn: test'
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'administrator#vs.dom:userpassword');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC );
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out;
dd($out);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://vcsa/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
The result is null.
this is a script word 100% using PHP
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/com/vmware/cis/session");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, 'username' . ":" . 'password');
$out = json_decode(curl_exec($ch));
// var_dump($out);
if ($out === false) {
echo 'Curl Error: ' . curl_error($ch);
exit;
}
$sid = $out->value;
curl_setopt($ch, CURLOPT_HTTPHEADER, array("vmware-api-session-id:$sid"));
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_URL, "https://{ip}/rest/vcenter/vm");
$output = curl_exec($ch);
$vms = json_decode($output);
var_dump($vms);
curl_close($ch);
?>
when you debug your code, the $out variable return null?
If yes, can you check if your curl returns 56 - OpenSSL SSL_read: Success error in $out = json_decode(curl_exec($ch));?
This error occoured with my code, and I see wich cURL in 7.67 version cause this trouble.
My solution was to downgrade cURL version to 7.65.
It works for me!
I had the same problem, and eventually understood that it is caused by this header:
curl_setopt($ch, CURLOPT_POST, true);
And replacing it with this one solves the issue:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
I am trying to add documents to elastic search index via bulk API. My CURL query works fine on command line and I converted it to PHP.
When I am trying to run PHP code, nothing happens. Neither documents are added in index in elastic search, nor any error appears.
CURL Query:
curl -H "Content-Type:application/json" -XPOST "http://localhost:9200/inven/default/_bulk?pretty" --data-binary "#file_name.json"
PHP Query:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:9200/inven/default/_bulk?pretty');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
'file' => '#' .realpath('file_name.json')
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
I have set ini_set('max_execution_time', 0); as well, in case it, it was timing out.
What could be the issue?
You are not sending the actual file contents, just its filename as a string. Try this:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:9200/inven/default/_bulk?pretty');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1 );
// The line below assumes the file_name.json is located in the same directory
// with this script, if not change the path to the file with the correct one.
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('file_name.json') );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
try $post = '#' .realpath('file_name.json')
If you are using PHP 5.5+, use:
$post = [
'file' => curl_file_create('file_name.json')
];
For more info check: Fix CURL file uploads.
i am wanting to post to an API in woocommerce php file i used code below.
$ch = curl_init("https://cors-anywhere.herokuapp.com/https://api.feverfinance.co.za/FTIntegration.svc/BalanceLookup");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Basic '. base64_encode( $username .":" .$password ) ,
'Accept: application/json, text/plain, */*' ,
'Content-Length: ' . strlen($data_string))
);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT , 0);
$result = curl_exec($ch);
// Check if any error occurred
if(curl_errno($ch))
{
print_r($ch);
print_r($result);
}
$obj = json_decode($result,true);
//Mage::log($result,true);
$tranactionSuccess = $obj['Success'];
$transactionMessage = $obj['Message'];
curl_close($ch);
when i print to console i get ErrorResource id #6.
Please can anyone advise me how to post to API in woocommerce. with the above information that is needed to post to the API
Regards
First you need to activate woocommerce APIs from wordpress backend and generate API keys
Check https://docs.woocommerce.com/document/woocommerce-rest-api/
Then use the following code based on your needs
Example: You need to retrieve product details
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/wp-json/wc/v3/products/794");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "consumer_key" . ":" . "consumer_secret");
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Note: consumer_key and consumer_secret was generated by API from backend
and 794 mean product ID
API docs for woocommerce http://woocommerce.github.io/woocommerce-rest-api-docs/
I need to capture the request headers and response while using PHP cURL to post data to external API call. The localhost page load shows in traffic where as the PHP cURL is not shown.
$url = "https://https://gds.eligibleapi.com/v1.3/enrollment.json";
$ch = curl_init(); // initialize curl handle
$user_agent = $_SERVER['HTTP_USER_AGENT'];
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_URL, $url); // set url to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // Fail on errors
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return into a variable
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); // add POST fields
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length: ' . strlen($data_string)));
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($ch); // if($data === false) echo 'Curl error: ' . curl_error($ch);
echo $data;
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Your URL is malformed (you have https://https://).
You need to set the Proxy option on the CURL command, e.g.
curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888');
See http://fiddler2.com/documentation/Configure-Fiddler/Tasks/ConfigurePHPcURL
You have to configure Fiddler to decrypt HTTPS traffic.
See here http://fiddler2.com/documentation/Configure-Fiddler/Tasks/DecryptHTTPS