I getting error upon submitting a post request using cURL. So far i have this code:
$login_url = 'http://192.168.1.1/login';
$postArr = array(
'username'=>'username',
'password'=>'password',
'dst' => base_url().'login/home'
);
$this->curlPost($login_url, $postArr);
function curlPost($url, $data){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo 'using curl --- '.$url.'<br />';
var_dump($server_output);
}
Basically, this code should do POST username and password to specific url, but unfortunately it is returning "Error 501: Not Implemented" my server is wamp. Anyone have idea what is causing this issue? Thanks
Related
When I try to run WooCommerce API in Postman it shows me json response, but when i try to run it will CURL, it doesn't giving me any response and loading, can anyone please help me what is issue of it ?
$header = array(
'Authorization: Basic Y2tfZmE1NWQ1N2RjNmY1xxxxxx'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,'https://xxxxx.com//wp-json/wc/v3/products/attributes');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
//curl_setopt($ch, CURLOPT_POST, 0);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "postvar1=value1&postvar2=value2&postvar3=value3");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
if (curl_error($ch)) {
$error_msg = curl_error($ch);
echo $error_msg; die;
}
echo "<pre>";
print_r($server_output);
die;
curl_close($ch);
I am trying to let people pay on my site with a simple API of Coinpayments (I tought it was simple). So, I found this page of the official Coinpayments website on how to create a transaction and receive money.
So, I am trying to receive the response as JSON en just echo it for now, so I can see what further steps I will take. Now, this is my code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://www.coinpayments.net/index.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"cmd=create_transaction&amount=".$amount."¤cy1=USD¤cy2=BTC&buyer_email=".$email."&version=1&key=b07f0fee01909b919235d58a950378b8c0e5266fa2174e007b24220a592aa92e&format=json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
curl_close ($ch);
When I try to echo the $server_output, it gives this: ERROR: Invalid command!. I have searched around, and I couldn't find anybody having the same issue as I do.
Thanks for the help! <3
This is what works for me:
function curl_coin_payment ($postdata = array()) {
$url = "https://www.coinpayments.net/api.php";
$payload = $postdata;
$payload['key'] = 'public_key';
$payload['version'] = 1;
$payload = http_build_query($payload, '', '&');
$api_secret = 'private_key';
$apiseal = hash_hmac('sha512', ($payload), $api_secret);
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ($payload)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
"HMAC: $apiseal",
"Content-Type: application/x-www-form-urlencoded",
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $request = curl_exec ($ch); curl_close ($ch);
return $request;
}
I am trying to used calendy API in the php I used following code to make check the Authentication Token
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://calendly.com/api/v1/echo");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-TOKEN: <Token-code>",// My Token code here
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch , CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch , CURLOPT_POST, true);
curl_setopt($ch , CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($ch , CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
print_r($result);?>
But I got error message 404 unauthorized
{
"type": "authentication_error",
"message": "Invalid token"
}
Any one who used Calendy API can used like this way
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://calendly.com/api/v1/echo");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"X-TOKEN: TOKE-HERE",
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch , CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch , CURLOPT_POST, true);
curl_setopt($ch , CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($ch , CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate
$result = curl_exec($ch);
echo $result;
curl_close ($ch);
I am learning PHP and trying to automate a website login and then post some data to another page once logged in. I managed to login to the website and when I try to post data, I got a "Document moved".
Then I analysed the headers in firebug and realised that there was a PHP_session_id, so when I tried to manually pass this PHP_session_id it worked.
So my question is, how can I automatically get the sessionid when I login and then subsequently pass this on to my second request?
This is what my code looks like:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/login-main.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"loginType=company&username=johndoe%40gmail.com&password=1234&company=test");
ob_start();
curl_exec ($ch);
ob_end_clean();
curl_close ($ch);
unset($ch);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Host:www.example.com',
'Origin:http://www.example.com',
'Cookie:PHPSESSID=na6ivnsdfab206ktb453o2au07',
'Referer:http://www.example.com/bookings/'
));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,"http://www.example.com/bookings.php");
curl_setopt($ch, CURLOPT_POSTFIELDS,
"start_date=&end_date=&type=instructor&b_type=&id=41");
$buf2 = curl_exec ($ch);
curl_close ($ch);
echo "<PRE>".htmlentities($buf2);
?>
Add to your code in all curl requests
curl_setopt($ch, CURLOPT_COOKIEJAR, $full_path_to_cookie_file);
curl_setopt($ch, CURLOPT_COOKIEFILE, $full_path_to_cookie_file);
For example
$full_path_to_cookie_file= __DIR__.'/cookie.txt';
I am using curl library to get the XML but getting error of connection with host. Following is my code, I have just removed the credentials.
$url="https://interface.callport.net:8080/P-RVWR-drcm01-cti/call/2142100570";
$curl_post_data = array(
"query_type" => 'caller_info',
"dnis" => '8883874944',
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, base64_encode('webapi#fastfix:color43t'));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo $result;
}
curl_close($ch);
It's SSL so this usually fixes the problem. cURL does not verify the certificate then.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
[edit] I solved the problems. First the SSL as described above, then I removed the base64 encoding of the username and password, changed so the data is sent by GET instead of POST and lastly fixed the headers.
$url="https://interface.callport.net:8080/P-RVWR-drcm01-cti/call/2142100570?query_type=caller_info&dnis=8883874944";
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'webapi#fastfix:color43t');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);