using curl for https connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
licksick
Forum Newbie
Posts: 6
Joined: Sat Nov 10, 2007 4:51 pm

using curl for https connection

Post by licksick »

I am trying unsuccessfully to send an xml request and receive an xml response from the UPS server to calculate shipping costs for a shopping cart I am building. I can curl http sites fine, but not https sites.

Code: Select all

$ch = curl_init(); // create cURL handle (ch)
if (!$ch) {
    die("Couldn't initialize a cURL handle");
} else {
	echo 'curl handle initialized<br />';
}
// set some cURL options
$ret = curl_setopt($ch, CURLOPT_URL,            "https://wwwcie.ups.com/ups.app/xml/Rate");
$ret = curl_setopt($ch, CURLOPT_HEADER,         1);
$ret = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
$ret = curl_setopt($ch, CURLOPT_TIMEOUT,        30);
$ret = curl_setopt($ch, CURLOPT_POSTFIELDS,		$xmlstr);

//   4 new lines trying to get https to work
$ret = curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,	false);
$ret = curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERPWD, "licksick:lsbforever");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

// execute
$ret = curl_exec($ch);

if (empty($ret)) {
    // some kind of an error happened
	echo '<br />some kind of an error happened<br />';
    die(curl_error($ch));
    curl_close($ch); // close cURL handler
} else {
    $info = curl_getinfo($ch);
    curl_close($ch); // close cURL handler
    if (empty($info['http_code'])) {
            die("No HTTP code was returned");
    } else {
        echo "The server responded: <br />";
        echo $info['http_code'];
    }
}
When I try this code on https sites I always get a connection timed out message with no other info. Am I missing something? Do I need to set some curl option to tell it to use port 443? I am new to curl and I am totally lost. I have trouble connecting regardless of whether I am sending straight xml or a string variable.
I am using:
php 5.1.4
libcurl 7.10.6
openssl 0.9.7a
Any help would be greatly appreciated!
rturner
Forum Newbie
Posts: 24
Joined: Sun Nov 04, 2007 1:39 pm

not me

Post by rturner »

I don't get a connection timed out.
Don't have $xmlstr so get malformed empty xml message.
When I comment the curl_setopt for $xmlstr I get same response as browser without credentials.

Do you have a firewall blocking port 443 on your side (can you browse to the site from the server running the php?)
licksick
Forum Newbie
Posts: 6
Joined: Sat Nov 10, 2007 4:51 pm

Post by licksick »

Do you have a firewall blocking port 443 on your side (can you browse to the site from the server running the php?)
I use godaddy as my hosting provider on a linux server (shared not dedicated). As far as I know there is no firewall blocking on the server. I can browse to the site from my home computer.
I get same response as browser without credentials
I'm really not even sure how to provide the proper credentials since UPS has an access license number, a username, and a password -- not just user & password.
rturner
Forum Newbie
Posts: 24
Joined: Sun Nov 04, 2007 1:39 pm

RSS-WW-XML.pdf

Post by rturner »

If you remove the curl_setopt with the xml variable and still get a timeout something is wrong on your host.



Page 30 of this file from the developer toolkit provides the example request message which includes your license number, userid, and password along with the format of the xml. The next page shows the response format you will get from them.


Download link
licksick
Forum Newbie
Posts: 6
Joined: Sat Nov 10, 2007 4:51 pm

Post by licksick »

...something is wrong on your host.
Ok, so forgive the noobie questions, but do I need a ssl certificate on my server to have a https connection with the UPS server?

Thank You rturner (and any others) for your help on this. I've really been stuck and the whole time I assumed it was either my code or the xml data being in a wierd format because I am new to curl and xml.
licksick
Forum Newbie
Posts: 6
Joined: Sat Nov 10, 2007 4:51 pm

Post by licksick »

It was the host after all. After extensive google searching using my host and curl as keywords I found this:


On our shared hosting, any CURL applications that make secure
HTTP connections (HTTPS over port 443) need to pass through a proxy server.
The address for this proxy server is [proxy address] and
connections must specify the use of port [port number].
The code below already includes this information.


They even gave me the proper PHP code for the secure connection using curl! I wish I'd found this information last week. Anyway the proplem is now RESOLVED. Thank you for your help.
Post Reply