Conversion from CURL to PHP Curl - php

I am running the below Curl request and it's working.
curl -v --cert /path/to/certs/myCert.crt --key /path/to/key/myKey.key --cacert /path/to/chainedcerts/intermediary.pem https://mysite:30009/
Converting it to php as below and it's failing with error 77 with no narration. I am thinking probably file permissions could be the issue. I am using nginx and the 3 certs are owned by nginx user. If there is any other way I can view the error description as I can see the other errors
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "xmlRequest=" . $xml);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Expect:','Content-Type: text/xml']);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSLCERT, "/path/to/certs/myCert.crt");
curl_setopt($ch, CURLOPT_SSLKEY, "/path/to/key/myKey.key");
curl_setopt($ch, CURLOPT_CAINFO, "/path/to/chainedcerts/intermediary.pem ");
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
Log::error($httpcode .' : '. curl_errno($ch) .' : '. curl_error($ch));
curl_close($ch);

Related

Failed to connect to curl URL

I am unable to connect with the Curl URL using PHP.
Below is my code, that I am trying to execute.
$url="http://b2b-dev.cenveo.com:6700/dmz/EZComm?fromTp=DiscountLabelsCustomer&toTp=DiscountLabels&operationID=DiscountLabels/1.0/PurchaseOrderRequest";
$xml_content = '<?xml version="1.0" encoding="utf-8"?><Order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.discountlabels.com http://www.discountlabels.com/DLI_INTGR.xsd" SubmittedDate="2017-12-06 02:52:43" xmlns="http://www.discountlabels.com"><Customer><Name>Cmagnets</Name><PSCustID>987654</PSCustID></Customer><BlindDropshipAddress AddressID=""><Name></Name><Attention></Attention><PostalAddress><Address1></Address1><Address2></Address2><City></City><State></State><PostalCode></PostalCode><Country></Country></PostalAddress><Email></Email><Telephone><PhoneNumber></PhoneNumber></Telephone>
<Fax></Fax><URL></URL></BlindDropshipAddress><Item ItemNumber="1"><Product><CustomDescription>Test product.</CustomDescription>
<ProductCode>1 x 4</ProductCode><Type>Labels</Type><SubType>Standard</SubType><Shape>Rectangle</Shape><ShapeType></ShapeType>
<ProductClass>L</ProductClass><Width>1.0</Width><Height>4.0</Height><Diameter>0.0000</Diameter></Product><Quantity>1000</Quantity><Ink>Black</Ink><Artwork><ArtPath></ArtPath><UserFilename></UserFilename></Artwork><PONumber>PO12345</PONumber>
<FirstLineOfCopy>Test Copy</FirstLineOfCopy><Shipping><Quantity>1000</Quantity><ShipMethod>Fedex</ShipMethod><ShipToAddress AddressID=""><Name>Name</Name><Attention>Sally Sunshine</Attention><PostalAddress><Address1>901 Some Street</Address1>
<Address2>Suite ABC</Address2><City>New Orleans</City><State>LA</State><PostalCode>39532</PostalCode><Country>US</Country></PostalAddress><Email>sally.sunshine#somecompany.com</Email><Telephone><PhoneNumber>5419876543</PhoneNumber><Extension /></Telephone><Fax><PhoneNumber></PhoneNumber><Extension /></Fax><URL></URL></ShipToAddress><Dropship>false</Dropship>
</Shipping><Stock><StockType></StockType><StockSubType></StockSubType><StockColor></StockColor><TintColor></TintColor>
<InsideWindow>false</InsideWindow></Stock><ConsecutiveDigit><BeginDigit></BeginDigit><EndDigit></EndDigit><DigitType></DigitType><SequenceType></SequenceType></ConsecutiveDigit><ItemSpecs><Attribute>Bleed</Attribute><Value>True</Value>
</ItemSpecs><CustPONumber></CustPONumber><Comments>Item Comments</Comments></Item></Order>';
$ch = curl_init();
url_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, 3);
curl_setopt($ch, CURLOPT_ENCODING, 'identity');
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type: application/xml", "Accept: application/xml"));
curl_setopt($ch, CURLOPT_FAILONERROR, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //times out after 4s
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_content); // add POST fields
curl_setopt($ch, CURLOPT_POST, 1);
$output = curl_exec($ch);
print_r($output);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
After Executing the above code I am getting the below Error message.
Error: Failed to connect to b2b-dev.cenveo.com port 6700: Connection refused.
Thanks in advance
Either you're missing some headers, or you have to whitelist your server's ip address with cenveo. There is no error related to curl, just the opposite server denying your connection.

cURL Commandline to php version

when I use the following on command line I am getting the output perfectly.
curl -X GET -H "Authorization: sso-key API_KEY:API_SECRET" "https://api.godaddy.com/v1/domains/mydomain.com"
but when I try to get this from PHP using the following code
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: sso-key API_KEY:API_SECRET"]);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, "API_KEY:API_SECRET");
$result=curl_exec ($ch);
curl_close ($ch);
var_dump($result);
I failed to get anything.
what am I missing here?
$URL = "https://api.godaddy.com/v1/domains/mydomain.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: sso-key API_KEY:API_SECRET"]);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
$result=curl_exec ($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
var_dump($result);
var_dump($httpCode);
curl_close ($ch);

SSL Certificate with PHP CURL

I'm using curl to send an xml file over https to the rightmove API - they supplied me with all the certificates.
I am getting the error :
60SSL certificate problem: unable to get local issuer
certificateResult =
I have tried everything i have found on every other stackoverflow post similar and nothing is working, i have tried downloading the old cacert.pem and changed the files in php.ini - ive installed my personal information file andcreated a certificate on the browser and local machine and nothing is removing the error 60.
This is my PHP :
<?php
$xml = file_get_contents("myxml.xml");
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__).'\mypem.pem');
curl_setopt($ch, CURLOPT_URL, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, 'https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_VERBOSE , 1);
$ch_result = curl_exec($ch);
print curl_errno($ch);
print curl_error($ch);
echo "Result = ".$ch_result;
curl_close($ch);
?>
this has had me banging my head for days, i would be very grateful for any assistance.
For my particular case i needed to add the keyfile, sslcert and cert password.
//$xml = file_get_contents("thexmlfile.xml");
$xml= $propertyXml->asXML();
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, getcwd() . '\pemfile.pem');
curl_setopt($ch, CURLOPT_URL, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSH_PRIVATE_KEYFILE, getcwd() . '\myjks.jks');
curl_setopt($ch, CURLOPT_SSLCERT, getcwd() . '\pemfile.pem');
curl_setopt($ch, CURLOPT_SSLCERTPASSWD, "thesslpassword");
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, "https://adfapi.adftest.rightmove.com/v1/property/sendpropertydetails");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_VERBOSE , 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$ch_result = curl_exec($ch);
print curl_errno($ch);
print curl_error($ch);
echo "Result = ".$ch_result;
curl_close($ch);
It is failing as curl is unable to verify the certificate provided by the server.
There are two options to get this to work:
1 Allows curl to make insecure connections, that is curl does not verify the certificate.
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
2 Add the root CA (the CA signing the server certificate) in php.ini
curl.cainfo=/path/to/cacert.pem
You should use option 2 as thats the option that ensures that you are connecting to secure ftp server.
I was getting the same "SSL certificate problem: unable to get local issuer certificateResult" error with HTTP GET to a remote https site. I only needed to provide the CA cert to php curl, ie CURLOPT_CAINFO:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CAINFO, "/path/to/CA_Bundle.crt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
if (curl_errno($ch)) {
$error_msg = curl_error($ch);
print "curl_error: $error_msg <br>";
} else {
print "output: $output<br>";
}
curl_close($ch);

XML Request Using CURL

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

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.

Categories