View Full Version : Sockets help
jmace
11-23-2010, 10:30 PM
Hey everyone!
I am trying to post a feed file to ebay from our site via a PHP script, but I am new to this stuff and having some trouble. On ebay's website they have an example of the request you need to send to post a feed file:
POST /path/to/upload/script HTTP/1.0
Connection: Keep-Alive
User-Agent: My Client App v1.0
Host:
https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload
Content-type: multipart/form-data;
boundary=THIS_STRING_SEPARATES
Content-Length: 256
--THIS_STRING_SEPARATES
Content-Disposition: form-data; name="token"
12345678987654321
--THIS_STRING_SEPARATES
Content-Disposition: form-data; name="file";
filename="listings.csv"
Content-Type: text/csv
... contents of listings.csv ...
--THIS_STRING_SEPARATES
I tried to implement it using this code:
<?php
// GET THE FEED FILE
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_USERAGENT => "website",
CURLOPT_AUTOREFERER => true,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYPEER => false,
);
$ch = curl_init('https://www.mysite.com/pull_feed.php');
curl_setopt_array( $ch, $options );
$contents = curl_exec( $ch );
curl_close( $ch );
//----------------------------------------------------//
//OPEN THE CONNECTION
$conn = fsockopen('https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeProgrammaticDownload', 80);
// START THE REQUEST
fputs($conn, "POST ".$_SERVER['PATH_INFO']." HTTP/1.0");
fputs($conn, "Connection: Keep-Alive");
fputs($conn, "User-Agent: My Client App v1.0");
fputs($conn, "Host:");
fputs($conn, "https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload");
fputs($conn, "Content-type: multipart/form-data;");
fputs($conn, "boundary=THIS_STRING_SEPARATES");
fputs($conn, "Content-Length: ".strlen($contents));
fputs($conn, "--THIS_STRING_SEPARATES");
fputs($conn, "Content-Disposition: form-data; name=\"token\"");
fputs($conn, "MY_KEY_IS_HERE_000000000000000000000000");
fputs($conn, "--THIS_STRING_SEPARATES");
fputs($conn, "Content-Disposition: form-data; name=\"file\";");
fputs($conn, "filename=\"listings.csv\"");
fputs($conn, "Content-Type: text/csv");
// SEND THE FILE
fputs($conn, $contents);
// END THE REQUEST
fputs($conn, "--THIS_STRING_SEPARATES");
// GET THE RESULT
while(!feof($conn)) {
echo fgets($conn, 128);
}
// CLOSE CONNECTION
fclose($conn);
?>
But I am getting this error:
Warning: fsockopen() [function.fsockopen]: unable to connect to https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeProgrammaticDownload:80 (Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?) in C:\home\imafs\public_html\funad\ebay\send_feed.php on line 27
Followed by more errors resulting from that one.
What can I do to solve this? Or is there another way to go about this?
djr33
11-24-2010, 06:20 AM
PHP is configured incorrectly on your server-- or at least it's not configured in the way that you need it.
Reinstall PHP if you can.
Create a page with the following line (only) to check your configuration:
<?php php_info(); ?>
If you are on a hosting account (rather than your own server) ask your host about this-- they might not allow it (or even block it), or they may be able to install it for you.
fsockopen doens't use https. try ssl:// instead.
supported sockets (http://us2.php.net/manual/en/transports.php)
jmace
11-24-2010, 05:30 PM
traq was right. That was one of the problems-- possibly the biggest. It's working better now with this code (at least it does something):
<?php
// GET THE FEED FILE
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "website", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false,
);
$ch = curl_init('https://www.mysite.com/pull_feed.php');
curl_setopt_array( $ch, $options );
$contents = curl_exec( $ch );
curl_close( $ch );
//----------------------------------------------------//
//OPEN THE CONNECTION
$conn = fsockopen('ssl://bulksell.ebay.com', 443);
// START THE REQUEST
fputs($conn, "POST ".$_SERVER['PHP_SELF']." HTTP/1.0\r\n");
fputs($conn, "Connection: Keep-Alive\r\n");
fputs($conn, "User-Agent: My Client App v1.0\r\n");
fputs($conn, "Host:\r\n");
fputs($conn, "https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload\r\n");
fputs($conn, "Content-type: multipart/form-data;\r\n");
fputs($conn, "boundary=THIS_STRING_SEPARATES\r\n");
fputs($conn, "Content-Length: ".strlen($contents)."\r\n");
fputs($conn, "--THIS_STRING_SEPARATES\r\n");
fputs($conn, "Content-Disposition: form-data; name=\"token\"\r\n");
fputs($conn, "MY_SECRET_TOKEN\r\n");
fputs($conn, "--THIS_STRING_SEPARATES\r\n");
fputs($conn, "Content-Disposition: form-data; name=\"file\";\r\n");
fputs($conn, "filename=\"listings.csv\"\r\n");
fputs($conn, "Content-Type: text/csv\r\n\r\n");
// SEND THE FILE
fputs($conn, $contents."\r\n");
// END THE REQUEST
fputs($conn, "--THIS_STRING_SEPARATES\r\n");
// GET THE RESULT
while(!feof($conn)) {
echo fgets($conn, 128);
}
// CLOSE CONNECTION
fclose($conn);
?>
But it is returning:
HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Location: http://pages.ebay.com/messages/page_not_found.html?eBayErrorEventName=p4apjlp%60jk%60jtb9%3Fjf.rdkhvjk357-2010.11.24.10.12.13.161.MST
Date: Wed, 24 Nov 2010 17:12:13 GMT
Connection: close
Possibly because I have to connect to ssl://bulksell.ebay.com instead of https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeProgrammaticDownload
You'll notice if you click the link that it's there and waiting for a file, but I'm not sending it there apparently. So now what should I do?
when I click your link I get a 404 page. are you sure this is the correct (and current) url to connect to?
jmace
11-24-2010, 09:45 PM
Did you click this link?
https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeProgrammaticDownload
Because this definitely is working for me. This is the page I am suppose to go to, from what I understand after reading this:
http://pics.ebaystatic.com/aw/pics/pdf/us/file_exchange/File_Exchange_Advanced_Instructions.pdf
(click "using advanced functions", then go to "using programmatic instructions")
Try sending the entire message at once, instead of using fputs() line-by-line.
(Also, ssl://www.example.com/page and https://www.example.com/page are not different pages; just different protocols.)
jmace
11-26-2010, 05:02 PM
Thank you very much for your help, traq.
I understand that ssl:// and https:// are the same page, but the problem is that I cannot connect to
https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeProgrammaticDownload
directly. I have to connect to page
bulksell.ebay.com or I get an error:
Warning: fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\home\public_html\send_feed.php on line 28
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeProgrammaticDownload:443 (Unknown error) in C:\home\public_html\send_feed.php on line 28
Warning: fputs(): supplied argument is not a valid stream resource in C:\home\public_html\send_feed.php on line 31
Warning: fputs(): supplied argument is not a valid stream resource in C:\home\public_html\send_feed.php on line 32
Warning: fputs(): supplied argument is not a valid stream resource in C:\home\public_html\send_feed.php on line 33
. . . .
But bulksell.ebay.com gives me a page not found from the server. . . .
HTTP/1.1 302 Moved Temporarily
Server: Apache-Coyote/1.1
Location: http://pages.ebay.com/messages/page_not_found.html?eBayErrorEventName=p4apjlp%60jk%60jtb9%3Fjf.rn77%28%3F66i%60eg-2010.11.26.10.07.10.028.MST
Date: Fri, 26 Nov 2010 17:07:10 GMT
Connection: close
I've been told that you have to do the fsockopen() to the domain, instead of the page-- which makes sense seeing as how trying to connect to the page gives me an error. But the domain isn't working either. I have no idea what to do. Any more help would be greatly appreciated.
jmace
11-26-2010, 05:49 PM
Whoops! I figured out the problem. It was a really stupid mistake on my part. :o I did in fact have to connect to the domain, not the path. But the
fputs($conn, "POST /ws/eBayISAPI.dll?FileExchangeProgrammaticDownload\r\n HTTP/1.1\r\n");
Code was wrong. Fixed now. But now I am getting
HTTP/1.1 400 Bad Request
Content-Length:54
Connection: close
<html><body><b>Http/1.1 Bad Request</b></body> </html>
As a response. . . . I'll have to see what's causing that, now.
Two steps forward, one step back.
I don't *think* there's supposed to be a newline between the path and the HTTP declaration.
Are you still using fputs line-by-line? Try assembling the entire message into a single fputs() call. If ebay is only getting part of your message, they likely just close the connection (that's your "bad request" error) instead of waiting for the whole thing.
jmace
11-29-2010, 06:59 PM
So instead of:
fputs($conn, "POST /ws/eBayISAPI.dll?FileExchangeProgrammaticDownload HTTP/1.1\r\n");
fputs($conn, "Connection: Keep-Alive\r\n");
fputs($conn, "User-Agent: My Client App v1.0\r\n");
. . . . .
I should use:
fputs($conn, $var_with_all_lines_including_file_to_send);
?
I've noticed that when the script "should" work, it just runs for a really long time and never actually does anything. I guess this would be the problem?
So instead of:
fputs($conn, "POST /ws/eBayISAPI.dll?FileExchangeProgrammaticDownload HTTP/1.1\r\n");
fputs($conn, "Connection: Keep-Alive\r\n");
fputs($conn, "User-Agent: My Client App v1.0\r\n");
. . . . .
I should use:
fputs($conn, $var_with_all_lines_including_file_to_send);
?
yes.
I've noticed that when the script "should" work, it just runs for a really long time and never actually does anything. I guess this would be the problem?
actually, I've run into that before. It may be an issue with your host (many have firewalls that will get in the way of this sort of connection; even though they may advertise that it's allowed. try asking them nicely).
jmace
11-29-2010, 10:00 PM
ARGH!!! I am so close and yet so far away!
Here is my code now:
<?php
set_time_limit(0);
// GET THE FEED FILE
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "fun-shop", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_SSL_VERIFYPEER => false,
);
$ch = curl_init('https://www.mysite.com/pull_feed.php');
curl_setopt_array( $ch, $options );
$contents = curl_exec( $ch );
curl_close( $ch );
//----------------------------------------------------//
//OPEN THE CONNECTION
$conn = fsockopen('ssl://bulksell.ebay.com', 443);
$request = "POST /ws/eBayISAPI.dll?FileExchangeUpload HTTP/1.1\r\n";
$request .= "Connection: Keep-Alive\r\n";
$request .= "User-Agent: My Client App v1.0\r\n";
$request .= "Host: bulksell.ebay.com\r\n";
$request .= "https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload\r\n";
$request .= "Content-type: multipart/form-data;\r\n";
$request .= "boundary=THIS_STRING_SEPARATES\r\n";
$request .= "Content-Length: ".strlen($contents)."\r\n\r\n";
$request .= "--THIS_STRING_SEPARATES\r\n";
$request .= "Content-Disposition: form-data;\r\n name=\"token\" \r\n";
$request .= "SECRET TOKEN HERE\r\n";
$request .= "Content-Disposition: form-data; name=\"file\";\r\n";
$request .= "filename=\"listings.csv\"\r\n";
$request .= "Content-Type: text/csv\r\n\r\n";
$request .= $contents;
$request .= "--THIS_STRING_SEPARATES\r\n";
// SEND THE REQUEST
fputs($conn, $request);
// GET THE RESULT
while(!feof($conn)) {
echo fgets($conn, 128);
}
// CLOSE CONNECTION
fclose($conn);
?>
The result is this:
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: dp1=bu1p/QEBfX0BAX19AQA**4ed554d7^; Domain=.ebay.com; Expires=Wed, 28-Nov-2012 21:55:35 GMT; Path=/
Set-Cookie: s=CgAD4ACBM9XLXOTlhMjNmMTgxMmMwYTIzMzk0YjI4OTQyZmZmZmQ2YTGNgGPD; Domain=.ebay.com; Path=/
Set-Cookie: nonsession=CgADKACBWWiLXOTlhMjNmMTgxMmMwYTIzMzk0YjI4OTQyZmZmZmQ2YTEAywABTPQoXzH9XCbM; Domain=.ebay.com; Expires=Tue, 29-Nov-2011 21:55:35 GMT; Path=/
Cache-Control: private
Pragma: no-cache
Content-Type: text/html;charset=UTF-8
Content-Length: 755
Date: Mon, 29 Nov 2010 21:55:35 GMT
<html>
<head>
<!--eBay V3- msxml 6.0 XXXXXXXXXXXXXXXXXXXXXXXXXX-->
<!--srcId - File Exchange Programmatically Upload-->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<title>Upload File Programmatically</title><script language="JavaScript"><!--
var pageName = "File Exchange Upload";
//--></script><script language="javascript" src="http://include.ebaystatic.com/js/e695/us/legacy/globals_e6951us.js"> </script><script src="http://include.ebaystatic.com/js/e695/us/legacy/common_functions_e6951us.js"> </script></head>
<body>
An unknown problem interrupted the file transfer. Please try again.<br><a href="javascript:void(0);" onclick="self.close();return false;">Close</a></body>
</html>
Any more ideas?
I tried contacting ebay support, and they were no help. They just bounced me around for an hour and then told me they don't offer support for these issues. . . .
how big is $contents ? maybe it's just taking a long time and something interrupted the connection. Your response doesn't seem to indicate anything else. How many times have you tried (this version) (and did you always get the same response)?
Also, should there be a line feed after $contents (before --THIS_STRING_SEPARATES)?
jmace
11-30-2010, 04:19 PM
Thank you for continuing to help me, traq. You've been a great help so far.
The $contents variable isn't that big. In my last test, it was 2490 characters. Files that other people are sending the same way are easily 50 times that size.
Here is the whole request that is being sent. I have played around with it, trying to add new lines in different places and such, but so far no luck. I doesn't matter how many times I try, I get the same error.
POST /ws/eBayISAPI.dll?FileExchangeUpload HTTP/1.1
Connection: Keep-Alive
User-Agent: My Client App v1.0
Host: bulksell.ebay.com
https://bulksell.ebay.com/ws/eBayISAPI.dll?FileExchangeUpload
Content-type: multipart/form-data;
boundary=THIS_STRING_SEPARATES
Content-Length: 2490
--THIS_STRING_SEPARATES
Content-Disposition: form-data; name="token"
MY_TOKEN_GOES_HERE
--THIS_STRING_SEPARATES
Content-Disposition: form-data; name="file";
filename="listings.csv"
Content-Type: text/csv
*Action(SiteID=US|Country=US|Currency=USD|Version=403|CC=UTF-8),"*Category","*Title","Subtitle","*Description","*ConditionID","PicURL","*Quantity","*Format","*StartPrice","BuyItNowPrice","*Duration","ImmediatePayRequired","*Location","GalleryType","PayPalAccepted","PayPalEmailAddress","PaymentInstructions","StoreCategory","ShippingDiscountProfileID","ShippingService-1:Option","ShippingService-1:Cost","ShippingService-1:Priority","ShippingService-1:ShippingSurcharge","ShippingService-2:Option","ShippingService-2:Cost","ShippingService-2:Priority","ShippingService-2:ShippingSurcharge","*DispatchTimeMax","CustomLabel","*ReturnsAcceptedOption","RefundOption","ReturnsWithinOption","ShippingCostPaidBy","AdditionalDetails","ShippingType","PackageType","PackageDepth","PackageLength","PackageWidth","WeightMajor","WeightMinor","WeightUnit","ShippingCarrierUsed","ShipFromZipCode"
"Add","52762","Noel Mouse Costume Mascot Medium","","This Noel Mouse w/Santa Coat & Hat Christmas Costume Mascot is merry as Santa\'s entourage and helper at your next Christmas corporate parade, Christmas play or Christmas theme park display. Our Noel Mouse w/Santa Coat & Hat Christmas Costume Mascot is created with a sewn and sculptured suit, matching head, two mittens and two shoe covers. ","1000","http://www.mysite.com/show_image.php?im=/img/01/01822a.jpg&nowater=1&size=500x500","1","FixedPrice","1195.00","","GTC","","77586","","1","ebay@mysite.com","","","","UPSGround","","","","","","","","1","01/01822AMD","ReturnsNotAccepted","","","","","Calculated","LargePackage","20","14","6","30","0","lb","UPSGround","77586"
--THIS_STRING_SEPARATES
I'm afraid I don't know, then...
Have you talked to your host (your host, not ebay) about socket connections (if they're allowed/ need any special configurations)? That's the only thing I can think of, at this point.
jmace
11-30-2010, 08:40 PM
Thank you again for all of your help. I guess I'm either not going to be able to do this or I'll have to pray someone on ebay decides to help me. (The latter doesn't seem likely, given their lack of help so far. :-p)
I'm sorry to ask again, but I think you may have misunderstood what I'm asking.
I'm not suggesting you ask ebay for help; I'm suggesting you ask your web host if there is anything (settings, firewalls, etc.) that may be interfering with the connections. If you've already done so, sorry - I missed it. But if you haven't, you should. Your host, and how they've configured your server, is a big piece of this puzzle.
:)
jmace
12-01-2010, 10:16 PM
No, your last post wasn't ambiguous-- I understood it. I've already talked to my web host and nothing should be keeping this code from working. Besides, I have a similar script that is sending feeds to Amazon, and it works perfectly fine in that context.
My comment was that I don't know of anything else that I can do for this script. Apparently it just isn't going to work.
ok, then. good luck, I hope you can figure it out!
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.