XML Request Using CURL - php

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

Related

Connecting to VCenter with PHP using REST API authentication error

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

No Response getting from Curl Request to https server

I am creating a PHP curl request to server and expecting some string in response but no response generating , What may be the reason for that?
Below is the Curl code for the same:
$fields = array("username"=> 'xxx',"domain"=> 'xxx',"password" => 'xxx');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://host:8090/add-user");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, $usr.':'.$pass);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if(!$result=curl_exec($ch)){
trigger_error(curl_error($ch));
}
curl_close($ch);
var_dump($result);
Below is equivalent command line request for the same:
curl --data 'username=xxx&domain=xxx&password=xxx' https://host:8090/add-user -k -v -u 'usr:pass'
What i am doing Wrong Here?
You should first try to use curl_setopt($ch, CURLOPT_VERBOSE, $file_handler); where $file_handler = fopen('somefile.log', 'a'). Thus you will see what exactly cURL does, and where it fails.

First Data Payment Gateway Issue

I'm trying to intigrate First data Payment Getway in my site
Config file for FirstData Payment Gateway
define("FDAPI_URL","https://ws.merchanttest.firstdataglobalgateway.com/fdggwsapi/services/order.wsdl");
define("FD_USERPWD","WSXXXXXXXX._.1:XXXXXXXX");
define("FD_SSLCERT", "/home/flagcases/domains/usaflagcases.com/public_html/certificate/WSXXXXXXXX._.1.pem");
define("FD_SSLKEY","/home/flagcases/domains/usaflagcases.com/public_html/certificate/WSXXXXXXXX._.1.key");
define("FD_SSLKEYPASSWD", "ckp_XXXXXXXX");
$ch = curl_init(FDAPI_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, FD_USERPWD);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSLCERT, FD_SSLCERT);
curl_setopt($ch, CURLOPT_SSLKEY, FD_SSLKEY);
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, FD_SSLKEYPASSWD);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if ($result === false)
{
echo curl_error($ch);
}
Alway I got Curl Error
unable to use client certificate (no key found or wrong pass phrase?) Curl Error
Please try to add WSXXXXXXXX..1.p12 might be help you... Where you added WSXXXXXXXX..1.pem and WSXXXXXXXX._.1.key

cURL XML Post issue

Im posting some XML to a webservice, the code I have written works in all other cases just not in this instance.
This is my code;
$ch = curl_init();
curl_setopt($ch, CURLOPT_POSTFIELDS, $string);
curl_setopt($ch, CURLOPT_ENCODING,'gzip');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 45);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml"));
$result = curl_exec($ch);
curl_close($ch);
return $result;
It keeps returning a 500 internal server error which is the same as if you go direct to the url so it's like the POST data isn't being sent. But I haven't check the content length and it seems to be..
Any Ideas?
try just this
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_string); // NOTE used $xml_string to indicate its just a text string of XML format
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
$result = curl_exec($ch);
curl_close($ch);

cURL Login to HTTPS site

I have been trying to post login credentials to an https site using cURL and PHP with no luck. Everything works fine for unsecured sites but I can't get it with https. I know the headers details that I am posting are correct (although I mocked them up for the sake of this example). Please help.
<?php
// Initialize cURL
$ch = curl_init('https://secured-example.com/auth.asp');
// Enable HTTP POST
curl_setopt($ch, CURLOPT_POST, 1);
// Use SSL
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// Set POST parameters
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=myUser&password=myPass');
// Imitate classic browser's behavior - handle cookies
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute 1st request
$store = curl_exec($ch);
// Set file to download
curl_setopt($ch, CURLOPT_URL, 'https://secured-example.com/file.pdf');
// Execute 2nd request (file download)
$content = curl_exec($ch);
curl_close($ch);
?>
Export the certificate.
Upload it to where your script can see it.
Then add:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/path/to/certificateCA.crt");
I used this once to connect to a bank account, hope this helps:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, HSBC_LINK1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$post_data = array('post1' => 'value');
$fields_string = '';
foreach($post_data as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string = rtrim($fields_string,'&');
curl_setopt($ch, CURLOPT_POST, count($post_data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$data1 = curl_exec($ch);

Categories