Alpha Video Training
Results 1 to 21 of 21

Thread: Help with using API and http_fetch

  1. #1
    Member richardurban's Avatar
    Real Name
    Richard Urban
    Join Date
    Apr 2000
    Location
    Harpers Ferry, WV
    Posts
    438

    Default Help with using API and http_fetch

    I am trying to interact with an API called ItemMaster (https://api.itemmaster.com/v2/api). I have read a number of posts, but am obviously missing some key points of how to do this.
    Here is code, modeled after this post by Steve Wood( http://www.alphasoftware.com/alphafo...t=encoding+url), that I have tried both on an A5W page and in a UX component on a button using an Ajax callback:
    Code:
    <%a5 
    debug(1)
    dim t_gatewayurl as c	= "https://api.itemmaster.com/v2/brand" 
    
    
    req = "<?xml version='1.0' encoding='utf-8' ?>" + crlf() + \
    "<requests xmlns='http://www.w3.org/2001/XMLSchema'>" + crlf() + \
    "<username:xxxxx>" + crlf() + \
    "<password:xxxxxx>" + crlf() + \
    "</requests>"
    
    dim url as c
    dim body as c
    url = t_gatewayurl
    dim pm as p
    dim pm.host as c = a5_split_url(url).protocol + "//" + a5_split_url(url).server
    dim pm.page as c = a5_split_url(url).page
    dim pm.port as n = 443
    dim pm.method as c = "post"
    dim pm.header as c ="username:xxxxx""password:xxxxxxxx"
    dim pm.header as c =
    dim pm.ssl_on as l = .t.
    dim pm.body as c = req
    resp = http_fetch(pm) 
    %>
    I get this error, or a similar one: "Error:Could not connect to server: WSAHOST_NOT_FOUND - Host not found while getting host name.. Detailed Description: No such host is known. The name is not an official host name or alias, or it cannot be found in the database(s) being queried. This error may also be returned for protocol and service queries, and means that the specified name could not be found in the relevant database.

    Error Number:16052.000000"
    I am also confused by where to put the user name and password. The API documentation says to set both the username and password as separate http header parameters. Is this done in the request body, or using the pm.header parameter? If the latter, what is the correct way to specify multiple header parameters?
    Richard Urban

    Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

  2. #2
    "Certified" Alphaholic
    Real Name
    Frank Bicknell
    Join Date
    Feb 2010
    Location
    39.1915° N, 84.4348° W
    Posts
    2,467

    Default Re: Help with using API and http_fetch

    I am not sure of what else is wrong but this will not work.

    Code:
    dim pm.header as c ="username:xxxxx""password:xxxxxxxx"
    
    Maybe 
    dim pm.header as c ="username:xxxxx,password:xxxxxxxx"
    or
    dim pm.header as c ="username:xxxxx password:xxxxxxxx"
    Win 10 64 Development, Win 7 64 WAS 11-1, 2, Win 10 64 AA-1,2, MySql, dbForge Studio The Best MySQL GUI Tool IMHO. http://www.devart.com/dbforge/mysql/studio/

  3. #3
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,759

    Default Re: Help with using API and http_fetch

    Normally... working with an API is, at once, both an exciting and frustrating experience. The "Standards" that all RESTful APIs purport to use could fill a barn. The only standard thing about API implementation is that they're all different. Part of the frustration is that most examples are in Ruby or PHP or C or Java. Well... we don't live in that world. Even a Javascript example would be helpful, but rarely provided. However, many times we get a cURL example, as we do in this API.

    Alpha, once again, to the rescue.

    Once you're in the XBasic editor, right click and select Genies... and then choose CURL command to XBasic. See? It's exciting already.

    Paste your cURL command into CURL command line - it must be all one long line. For example... (itemMaster includes a $, but get rid of that)

    Code:
    curl --header "username:xxx" --header "password:yyy" 'https://api.itemmaster.com/v2/manufacturer'
    and click the Generate XBasic button. You'll get the results below. I've commented out the SSH_KNOWNHOSTS line... you don't need that.

    The returning data is in either the cf_1.GetHeaders() call or the cf_1.GetContent() call... probably content. Assign each to a pointer variable and see what's inside.

    Thank goodness Alpha has put cURL to XBasic in place.


    Code:
    dim cf_1 as extension::CurlFile
    dim flag_1 as l
    dim slist1[0] as c = [ "username:xxx" , "password:yyy" ]
    dim ce as extension::Curl
    
    ce = extension::Curl.Init()
    ce.setOpt("URL","https://api.itemmaster.com/v2/manufacturer")
    ce.setOpt("NOPROGRESS",1)
    ce.setOpt("USERAGENT","curl/7.34.0")
    ce.setOpt("HTTPHEADER",slist1)
    ce.setOpt("MAXREDIRS",50)
    ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
    ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
    ''''ce.setOpt("SSH_KNOWNHOSTS","C:\Users\David\AppData\Roaming/_ssh/known_hosts")
    ce.setOpt("TCP_KEEPALIVE",1)
    ce.SetOpt("FILE",cf_1)		
    flag_1 = ce.Exec()
    if flag_1 then
         showvar( "Headers: "+crlf()+cf_1.GetHeaders()+crlf()+"Content:"+crlf()+cf_1.GetContent())
    else 
    	 showvar("error: " + ce.Error() )
    end if
    ce.close()

  4. #4
    Alpha Software Employee Lenny Forziati's Avatar
    Real Name
    Lenny Forziati
    Join Date
    Nov 2001
    Location
    Alpha Software
    Posts
    4,695

    Default Re: Help with using API and http_fetch

    Your problem is here:
    Code:
    dim pm.host as c = a5_split_url(url).protocol + "//" + a5_split_url(url).server
    The host should be a hostname only, not a protocol and a hostname. The error you are getting is telling you that you have supplied an invalid hostname. Change your code to
    Code:
    dim pm.host as c = a5_split_url(url).server
    There is also a problem with pm.hostname as Frank pointed out, which will likely cause the next problem for you once you fix the host.
    Last edited by Lenny Forziati; 03-19-2015 at 05:58 PM.

    Lenny Forziati
    Vice President, Internet Products and Technical Services
    Alpha Software Corporation

  5. #5
    Member richardurban's Avatar
    Real Name
    Richard Urban
    Join Date
    Apr 2000
    Location
    Harpers Ferry, WV
    Posts
    438

    Default Re: Help with using API and http_fetch

    I could not get the code in the first post to work after correcting the host: the message returned is:
    Response: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>302 Found</title>
    </head><body>
    <h1>Found</h1>
    <p>The document has moved <a href="https://api.itemmaster.com/v2">here</a>.</p>
    </body></html>

    I was able to get the Curl command to run properly from the windows command screen, when using the --insecure command line option like this:
    Code:
    curl --insecure --header "username:xxx" --header "password:yyy" 'https://api.itemmaster.com/v2/brand'
    Without the --insecure, the command line said there was a problem validating the certificate file.

    Using the Curl genie with the above code, I get the following code (after manually adding in the CA INFO and CA Path, which were wrong)

    Code:
    dim cf_1 as extension::CurlFile
    dim cf_2 as extension::CurlFile
    dim cf_3 as extension::CurlFile
    dim cf_4 as extension::CurlFile
    dim cf_5 as extension::CurlFile
    dim flag_1 as l
    dim flag_2 as l
    dim flag_3 as l
    dim flag_4 as l
    dim flag_5 as l
    dim slist1[0] as c = [ "username:xxx" , "password:xxx" ]
    dim slist2[0] as c = [ "username:xxx" , "password:xxx" ]
    dim slist3[0] as c = [ "username:xxx" , "password:xxx" ]
    dim slist4[0] as c = [ "username:xxx" , "password:xxx" ]
    dim slist5[0] as c = [ "username:xxx" , "password:xxx" ]
    dim ce as extension::Curl
    
    ce = extension::Curl.Init()
    ce.setOpt("URL","https://api.itemmaster.com/v2/brand")
    ce.setOpt("NOPROGRESS",1)
    ce.setOpt("USERAGENT","curl/7.33.0")
    ce.setOpt("HTTPHEADER",slist1)
    ce.setOpt("MAXREDIRS",50)
    ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
    ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
    ce.setOpt("SSL_VERIFYPEER",0)
    ce.setOpt("SSL_VERIFYHOST",0)
    ce.setOpt("TCP_KEEPALIVE",1)
    ce.SetOpt("FILE",cf_1)		
    flag_1 = ce.Exec()
    ce.setOpt("URL","Files")
    ce.setOpt("NOPROGRESS",1)
    ce.setOpt("USERAGENT","curl/7.33.0")
    ce.setOpt("HTTPHEADER",slist2)
    ce.setOpt("MAXREDIRS",50)
    ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
    ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
    ce.setOpt("SSL_VERIFYPEER",0)
    ce.setOpt("SSL_VERIFYHOST",0)
    ce.setOpt("TCP_KEEPALIVE",1)
    ce.SetOpt("FILE",cf_2)		
    flag_2 = ce.Exec()
    ce.setOpt("URL","(x86)\a5V12\caroot\ca-cert.pem")
    ce.setOpt("NOPROGRESS",1)
    ce.setOpt("USERAGENT","curl/7.33.0")
    ce.setOpt("HTTPHEADER",slist3)
    ce.setOpt("MAXREDIRS",50)
    ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
    ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
    ce.setOpt("SSL_VERIFYPEER",0)
    ce.setOpt("SSL_VERIFYHOST",0)
    ce.setOpt("TCP_KEEPALIVE",1)
    ce.SetOpt("FILE",cf_3)		
    flag_3 = ce.Exec()
    ce.setOpt("URL","Files")
    ce.setOpt("NOPROGRESS",1)
    ce.setOpt("USERAGENT","curl/7.33.0")
    ce.setOpt("HTTPHEADER",slist4)
    ce.setOpt("MAXREDIRS",50)
    ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
    ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
    ce.setOpt("SSL_VERIFYPEER",0)
    ce.setOpt("SSL_VERIFYHOST",0)
    ce.setOpt("TCP_KEEPALIVE",1)
    ce.SetOpt("FILE",cf_4)		
    flag_4 = ce.Exec()
    ce.setOpt("URL","(x86)\a5V12\caroot")
    ce.setOpt("NOPROGRESS",1)
    ce.setOpt("USERAGENT","curl/7.33.0")
    ce.setOpt("HTTPHEADER",slist5)
    ce.setOpt("MAXREDIRS",50)
    ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
    ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
    ce.setOpt("SSL_VERIFYPEER",0)
    ce.setOpt("SSL_VERIFYHOST",0)
    ce.setOpt("TCP_KEEPALIVE",1)
    ce.SetOpt("FILE",cf_5)		
    flag_5 = ce.Exec()
    if flag_1 then
         showvar( "Headers: "+crlf()+cf_1.GetHeaders()+crlf()+"Content:"+crlf()+cf_1.GetContent())
    else 
    	 showvar("error: " + ce.Error() )
    end if
    if flag_2 then
         showvar( "Headers: "+crlf()+cf_2.GetHeaders()+crlf()+"Content:"+crlf()+cf_2.GetContent())
    else 
    	 showvar("error: " + ce.Error() )
    end if
    if flag_3 then
         showvar( "Headers: "+crlf()+cf_3.GetHeaders()+crlf()+"Content:"+crlf()+cf_3.GetContent())
    else 
    	 showvar("error: " + ce.Error() )
    end if
    if flag_4 then
         showvar( "Headers: "+crlf()+cf_4.GetHeaders()+crlf()+"Content:"+crlf()+cf_4.GetContent())
    else 
    	 showvar("error: " + ce.Error() )
    end if
    if flag_5 then
         showvar( "Headers: "+crlf()+cf_5.GetHeaders()+crlf()+"Content:"+crlf()+cf_5.GetContent())
    else 
    	 showvar("error: " + ce.Error() )
    end if
    ce.close()
    However, unlike from the command line, this does not run, instead I get the get the attached error saying "Function 'not valid' was not recognized".

    One last point, I noticed that in the windows command line you have to use double quotes around the URL, instead of single quotes. However, the x-basic genie comes up with double quotes around single quotes for the URL; is that right? I also tried with just the double quotes.
    "
    "
    Last edited by richardurban; 01-17-2015 at 05:26 AM.
    Richard Urban

    Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

  6. #6
    Member richardurban's Avatar
    Real Name
    Richard Urban
    Join Date
    Apr 2000
    Location
    Harpers Ferry, WV
    Posts
    438

    Default Re: Help with using API and http_fetch

    Here is the error mentioned above: curl error1.JPG
    Richard Urban

    Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

  7. #7
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,759

    Default Re: Help with using API and http_fetch

    I'm not sure where you're getting the "--insecure" stuff, but if you take itemMaster's cURL example...

    Code:
    curl --header "username:xxx" --header "password:yyy" 'https://api.itemmaster.com/v2/brand'
    The genie will produce this code...

    Code:
    dim cf_1 as extension::CurlFile
    dim flag_1 as l
    dim slist1[0] as c = [ "username:xxx" , "password:xxx" ]
    dim ce as extension::Curl
    
    ce = extension::Curl.Init()
    ce.setOpt("URL","https://api.itemmaster.com/v2/brand")
    ce.setOpt("NOPROGRESS",1)
    ce.setOpt("USERAGENT","curl/7.33.0")
    ce.setOpt("HTTPHEADER",slist1)
    ce.setOpt("MAXREDIRS",50)
    ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
    ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
    ce.setOpt("SSL_VERIFYPEER",0)
    ce.setOpt("SSL_VERIFYHOST",0)
    ce.setOpt("TCP_KEEPALIVE",1)
    ce.SetOpt("FILE",cf_1)		
    flag_1 = ce.Exec()
    
    if flag_1 then
    	dim cfh1 as c
    	dim cfc1 as c
    	cfh1 = cf_1.GetHeaders()
    	cfc1 = cf_1.GetContent()	
    end if	
    ce.close()
    If you're getting more than one set of vars and code then something is wrong with your cURL command.

    The error you're getting is because showvar is only valid in the Interactive window. If you want to run your code there, then fine. If you're running your code in a function, then replace the showvar stuff with code as in the sample above. My code is in the "if flag_1" test. Just assign the header and content data to variables to easily check what's inside.

  8. #8
    Volunteer Moderator Steve Wood's Avatar
    Real Name
    Steve Wood
    Join Date
    Nov 2003
    Location
    Bay Area, California
    Posts
    8,842

    Default Re: Help with using API and http_fetch

    With Alpha adding CURL is there now something fundamentally wrong with using the much more simple syntax in the first post, http_fetch(pm)? CURL has been available to Alpha apps since the beginning of time but up to this point everyone used http_fetch() or http_post() to address web services.
    Steve Wood
    Join the ALPHA DEVELOPERS NETWORK
    There is no Cloud. It's just someone else's computer.
    Web - Mobile - Hosting - Products - Frameworks - Developer Resources
    AlphaToGo | IADN (100% Alpha Anywhere Websites)

  9. #9
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,759

    Default Re: Help with using API and http_fetch

    It really depends (IMO) on the site, as to how easy or hard getting the fetch or post to work. For someone who is new to RESTful services, the cURL to XBasic Genie gets you there a lot faster. Most RESTful sites give you a cURL example and not a simple Fetch or Post example. It's tough, at times, to work out what goes in the header, what goes in the body, etc.

  10. #10
    Member
    Real Name
    Jim Coltz
    Join Date
    Oct 2005
    Location
    Western Pennsylvania
    Posts
    600

    Default Re: Help with using API and http_fetch

    Rich,

    showvar() is a desktop only function and will error out on the web side. Running the code you show, with the showvar() method, from the desktop, produces an "error: Couldn't resolve host name".
    Jim Coltz
    Alpha Custom Database Solutions, LLC
    A5CustomSolutions.com
    jimcoltz@a5customsolutions.com

  11. #11
    Member richardurban's Avatar
    Real Name
    Richard Urban
    Join Date
    Apr 2000
    Location
    Harpers Ferry, WV
    Posts
    438

    Default Re: Help with using API and http_fetch

    This works in the interactive window with the new shell command functionality:
    Code:
    $curl --insecure --header  "username:xxx" --header "password:xxxxx" "https://api.itemmaster.com/v2/item?upc=012000004926&epf=50" --output "C:\Temp\testfile.txt"
    I then get a retun file with the url of the image to download.
    However, I don't know how to download the image: when I try this
    Code:
     --insecure  --header  "xxxxx" -- --header "xxxxxx" "http://media.itemmaster.com/0/0/0/401/c4b13721-eb7d-4251-995a-bc5f3c8bd925.jpg" -o "c:\temp\picturetest.jpg"
    A message is returned saying:
    HTTP Status 407 The client must first authenticate itself with the proxy.
    Does anyone know the correct format to use to first authenticate with CURL and then download the image at a given url?
    Richard Urban

    Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

  12. #12
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,759

    Default Re: Help with using API and http_fetch

    Hey Richard,

    I think this is what you're looking for. This falls under the heading of... sometimes... this stuff is just too much fun. Imagine there being a web service that does this. Too funny.

    http://www.youtube.com/watch?v=9EZhxP3MmEc

    I'll package and post the code.

  13. #13
    Member
    Real Name
    Craig Schumacker
    Join Date
    May 2000
    Posts
    690

    Default Re: Help with using API and http_fetch

    Very nice.... A tremendous amount of potential.

    If only they captured tare weight, too...

  14. #14
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,759

    Default Re: Help with using API and http_fetch

    It seems they have it, but it's not included in the xml data that I can see. E.g. a 1lb box of Ritz crackers has the packaging listed at 1.195lb. They only list 1lb in the xml data. They'd probably add it to the xml if asked.

  15. #15
    Member
    Real Name
    Craig Schumacker
    Join Date
    May 2000
    Posts
    690

    Default Re: Help with using API and http_fetch

    I enjoyed their video (on YouTube) where they created a virtual PeaPod store.

    I can think of 100 uses for such an API if it was nearly complete. It looks like they have a long way to go. Some products, like Heinz, are near 100%. Some, Like Absolut, are just a single of dozens of retail items.

    I wonder why they stopped at liquor and food? Why not any retail item?

    Anyway, defineatly going in the right direction.

  16. #16
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    7,759

    Default Re: Help with using API and http_fetch

    Attached is the UX component for this process. The List control is using a connection and a table... as is an XBasic function. The table definition can be found at the beginning of the XBasic function in the UX.

    ItemMaster returns data for a Brand, or a Search, or a UPC, and other queries. In this example we're going after a UPC list from a table. As each call is made to ItemMaster for a UPC the data for that UPC is returned in XML format which includes URLs (multiple) for product images. In this example we're just going after 1 image.

    Once we have the image URL, we do an http_get to grab that image data. Once you've got the binary data you can write it out to disk or push it out to sql.

    I feel this is an interesting example because we get to use a List control, an API, XML data, cURL, http_get, and binary image data.
    Attached Files Attached Files

  17. #17
    Member richardurban's Avatar
    Real Name
    Richard Urban
    Join Date
    Apr 2000
    Location
    Harpers Ferry, WV
    Posts
    438

    Default Re: Help with using API and http_fetch

    David, thanks so much for taking this on. As soon as I work with it I will report back.
    Richard Urban

    Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

  18. #18
    Member richardurban's Avatar
    Real Name
    Richard Urban
    Join Date
    Apr 2000
    Location
    Harpers Ferry, WV
    Posts
    438

    Default Re: Help with using API and http_fetch

    Thanks again, David. This works beautifully. As I work with it I will get a much better understanding of how to work with APIs.
    Richard Urban

    Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

  19. #19
    Member richardurban's Avatar
    Real Name
    Richard Urban
    Join Date
    Apr 2000
    Location
    Harpers Ferry, WV
    Posts
    438

    Default Re: Help with using API and http_fetch

    Here is David's code modified to save the file and to make two resized images with the a5 image magic function:
    Code:
    function getItemMaster as c (e as p)
    	
    'debug(1)
    
    'Here is the MySQL Table structure for the table used in this function
    'SET FOREIGN_KEY_CHECKS=0;
    'DROP TABLE IF EXISTS `tblitemmasterimages`;
    'CREATE TABLE `tblitemmasterimages` (
    '  `ItemId` int(10) NOT NULL AUTO_INCREMENT,
    '  `ItemUPC` varchar(20) DEFAULT NULL,
    '  `ItemDesc` varchar(255) DEFAULT NULL,
    '  `ItemImageURL` varchar(500) DEFAULT NULL,
    '  `ItemImage` longblob,
    '  PRIMARY KEY (`ItemId`)
    ') ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
    
    
    dim cn as sql::Connection
    dim flag as l
    dim sqlStmt as c
    dim rs as sql::ResultSet
    dim args as sql::arguments
    
    flag = cn.open("::Name::urbangrocery")
    	args.set("from",e.dataSubmitted.from_code)
    	args.set("to",e.dataSubmitted.to_code)
    
    if flag then
    
    	sqlStmt = "SELECT UPC from products where new_code between :from and :to and (new_field2<>'Office' or new_field2 is null);"
    
    	flag = cn.Execute(sqlStmt,args)
    	if flag then
    		'ALL OK
    		rs = cn.ResultSet
    		myUPCList = rs.ToString()
    	else
    		'there was an error - close the connection and exit
    		cn.Close()
    		dim msg as c 
    		msg = "Could not execute the query. Error reported was: " + cn.CallResult.text
    		msg = js_escape(msg)
    		dim jscmd as c 
    		jscmd = "alert('" + msg + "');"
    		getItemMaster = jscmd
    		exit function
    	end if  
    	
    else
    	'there was an error - close the connection and exit
    	dim msg as c 
    	msg = "Could not open the connection. Error reported was: " + cn.CallResult.text
    	msg = js_escape(msg)
    	dim jscmd as c 
    	jscmd = "alert('" + msg + "');"
    	getItemMaster = jscmd
    	exit function
    end if
    
    'cn.FreeResult()
    
    
    'Set up a list of UPCs to get
    'dim myUPCList as c = <<%str%
    '012000005190
    '012000004926
    '012000008931
    '012000004995
    '078000160468
    '078000156461
    '078000152456
    '078000152463
    '078000003000
    '078000001143
    '%str%
    
    dim imURL as c
    dim imContentXML as c
    dim imContent as p
    dim imCount as n
    dim imName as c
    dim imImage as b
    
    dim flag_1 as l
    dim slist1[0] as c = [ "username:xxxxxx" , "password:xxxxxxx" ]
    dim ce as extension::Curl
    ce = extension::Curl.Init()
    ce.setOpt("NOPROGRESS",1)
    ce.setOpt("USERAGENT","curl/7.34.0")
    ce.setOpt("HTTPHEADER",slist1)
    ce.setOpt("MAXREDIRS",50)
    ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
    ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
    'ce.setOpt("SSH_KNOWNHOSTS","C:\Users\David\AppData\Roaming/_ssh/known_hosts")
    ce.setOpt("TCP_KEEPALIVE",1)
    
    'debug(1)
    for each imItem in myUPCList
    
    	currUPC = imItem.value
    	dim cf_1 as extension::CurlFile	
    	ce.setOpt("URL","https://api.itemmaster.com/v2/item?idx=0&limit=500&upc=" + currUPC + "&epl=600&ef=jpg")	
    	ce.SetOpt("FILE",cf_1)		
    
    	flag_1 = ce.Exec()
    
    	'get the reulting xml from the call
    	imContentXML = cf_1.GetContent()
    
    	'turn the xml into something nice we can use
    	delete imContent
    	dim imContent as p
    	imContent = *property_from_xml(imContentXML,imContent) 
    	
    	imCount = imContent.items.count
    	if imCount <> 0 then
    
    		'we have data from the UPC code
    		
    		'media is an array of images.  Just grab the 1st one.  Another loop is needed to get all images for a product
    		delete imURL
    		dim imURL as c
    		imURL = imContent.items.item.media.medium.url
    		imName = imContent.items.item.name
    	
    		'The returned image url is messed up for our next call because of &amp; which is included
    		'in the returned URL - fix it.
    		imURL = stritran(imURL,"&amp;","&")
    	
    
    		'The URL is a simple http call... use http_get.  The binary image data is returned in the body
    		dim result as p
    		result = http_get(imURL)
    	
    		'The image is binary - write it out to a file... or to a table
    		imImage = result.body
    		
    		file.from_blob("C:\a5\Ver12web\tempimages\\" + currUPC + ".jpg", imImage)  
    		
    		sourceFile = "C:\a5\Ver12web\tempimages\\" + currUPC + ".jpg"
    		destinationFile = "C:\a5\Ver12web\images\\" + currUPC + ".jpg"
    		
    		a5_runImageMagicCommand(sourceFile,destinationFile,"-resize 50x50")
    		
    		destinationFile = "C:\a5\Ver12web\images\full\\" + currUPC + ".jpg"
    		a5_runImageMagicCommand(sourceFile,destinationFile,"-resize 350x350")
    		file.remove(sourceFile)
    		'Write it back out to the table
    		'debug(1)
    		
    		args.set("argsUPC",currUPC)
    		args.set("argsURL",imURL)
    		args.set("pic_url","http://ugimg.com/images/" + currUPC + ".jpg")
    				
    		sqlStmt = <<%str%
    		 	UPDATE products
    		 	set picture_ref = :argsURL, picture_url = :pic_url,new_field2 = 'Office',upload_by = 'itemmaster'
    			WHERE UPC = :argsUPC;
    			%str%
    
    		flag = cn.Execute(sqlStmt,args)
    		if flag then
    			'ALL OK
    		else
    			'there was an error - close the connection and exit
    			cn.Close()
    			dim msg as c 
    			msg = "Could not execute the query. Error reported was: " + cn.CallResult.text
    			msg = js_escape(msg)
    			dim jscmd as c 
    			jscmd = "alert('" + msg + "');"
    			getItemMaster = jscmd
    			exit function
    		end if  
    	
    	end if
    	delete cf_1
    	
    next  
    
    
    cn.close()
    ce.close()
    
    'refresh the UX List control
    'getItemMaster = "{dialog.object}.refreshListData('ItemMasterList');"
    getItemMaster = "alert('All Item Master matching items have been downloaded');"
    
    	
    	
    end function
    One problem is that once in a while there is data returned, but no image. I am not sure how to test if
    " imContent.items.item.media.medium.url" exists and then skip to the next upc in the loop. Can anyone help with that?
    Last edited by richardurban; 03-29-2015 at 02:11 AM.
    Richard Urban

    Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

  20. #20
    Member richardurban's Avatar
    Real Name
    Richard Urban
    Join Date
    Apr 2000
    Location
    Harpers Ferry, WV
    Posts
    438

    Default Re: Help with using API and http_fetch

    I have added an "on error resume" statement to ignore the error message when a picture is missing and to continue with the next item. This solves the problem of an occasional missing picture, even though there is a listing for that item:
    Code:
    function getItemMaster as c (e as p)
    	
    'debug(1)
    
    'Here is the MySQL Table structure for the table used in this function
    'SET FOREIGN_KEY_CHECKS=0;
    'DROP TABLE IF EXISTS `tblitemmasterimages`;
    'CREATE TABLE `tblitemmasterimages` (
    '  `ItemId` int(10) NOT NULL AUTO_INCREMENT,
    '  `ItemUPC` varchar(20) DEFAULT NULL,
    '  `ItemDesc` varchar(255) DEFAULT NULL,
    '  `ItemImageURL` varchar(500) DEFAULT NULL,
    '  `ItemImage` longblob,
    '  PRIMARY KEY (`ItemId`)
    ') ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
    
    
    dim cn as sql::Connection
    dim flag as l
    dim sqlStmt as c
    dim rs as sql::ResultSet
    dim args as sql::arguments
    
    flag = cn.open("::Name::urbangrocery")
    	args.set("from",e.dataSubmitted.from_code)
    	args.set("to",e.dataSubmitted.to_code)
    
    if flag then
    
    	sqlStmt = "SELECT UPC from products where new_code between :from and :to and (new_field2<>'Office' or new_field2 is null);"
    
    	flag = cn.Execute(sqlStmt,args)
    	if flag then
    		'ALL OK
    		rs = cn.ResultSet
    		myUPCList = rs.ToString()
    	else
    		'there was an error - close the connection and exit
    		cn.Close()
    		dim msg as c 
    		msg = "Could not execute the query. Error reported was: " + cn.CallResult.text
    		msg = js_escape(msg)
    		dim jscmd as c 
    		jscmd = "alert('" + msg + "');"
    		getItemMaster = jscmd
    		exit function
    	end if  
    	
    else
    	'there was an error - close the connection and exit
    	dim msg as c 
    	msg = "Could not open the connection. Error reported was: " + cn.CallResult.text
    	msg = js_escape(msg)
    	dim jscmd as c 
    	jscmd = "alert('" + msg + "');"
    	getItemMaster = jscmd
    	exit function
    end if
    
    'cn.FreeResult()
    
    
    'Set up a list of UPCs to get
    'dim myUPCList as c = <<%str%
    '012000005190
    '012000004926
    '012000008931
    '012000004995
    '078000160468
    '078000156461
    '078000152456
    '078000152463
    '078000003000
    '078000001143
    '%str%
    
    dim imURL as c
    dim imContentXML as c
    dim imContent as p
    dim imCount as n
    dim imName as c
    dim imImage as b
    
    dim flag_1 as l
    dim slist1[0] as c = [ "username:rurban" , "password:Password1" ]
    dim ce as extension::Curl
    ce = extension::Curl.Init()
    ce.setOpt("NOPROGRESS",1)
    ce.setOpt("USERAGENT","curl/7.34.0")
    ce.setOpt("HTTPHEADER",slist1)
    ce.setOpt("MAXREDIRS",50)
    ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
    ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
    'ce.setOpt("SSH_KNOWNHOSTS","C:\Users\David\AppData\Roaming/_ssh/known_hosts")
    ce.setOpt("TCP_KEEPALIVE",1)
    
    'debug(1)
    for each imItem in myUPCList
    
    	currUPC = imItem.value
    	dim cf_1 as extension::CurlFile	
    	ce.setOpt("URL","https://api.itemmaster.com/v2/item?idx=0&limit=500&upc=" + currUPC + "&epl=600&ef=jpg")	
    	ce.SetOpt("FILE",cf_1)		
    
    	flag_1 = ce.Exec()
    
    	'get the reulting xml from the call
    	imContentXML = cf_1.GetContent()
    
    	'turn the xml into something nice we can use
    	delete imContent
    	dim imContent as p
    	imContent = *property_from_xml(imContentXML,imContent) 
    	
    	imCount = imContent.items.count
    	if imCount <> 0 then
    
    		'we have data from the UPC code
    		
    		'media is an array of images.  Just grab the 1st one.  Another loop is needed to get all images for a product
    		delete imURL
    		dim imURL as c
    		on error resume error_handler
    		imURL = imContent.items.item.media.medium.url
    		
    		imName = imContent.items.item.name
    	
    		'The returned image url is messed up for our next call because of &amp; which is included
    		'in the returned URL - fix it.
    		imURL = stritran(imURL,"&amp;","&")
    	
    
    		'The URL is a simple http call... use http_get.  The binary image data is returned in the body
    		dim result as p
    		result = http_get(imURL)
    	
    		'The image is binary - write it out to a file... or to a table
    		imImage = result.body
    		
    		'file.from_blob("C:\Temp\tempimage\\" + currUPC + ".jpg", imImage)
    		file.from_blob("C:\a5\Ver12web\tempimages\\" + currUPC + ".jpg", imImage)  
    		'sleep(1)
    		'sourceFile = "C:\Temp\tempimage\\" + currUPC + ".jpg"
    		'destinationFile = "C:\Temp\newimage\\" + currUPC + ".jpg"
    		sourceFile = "C:\a5\Ver12web\tempimages\\" + currUPC + ".jpg"
    		destinationFile = "C:\a5\Ver12web\images\\" + currUPC + ".jpg"
    		
    		a5_runImageMagicCommand(sourceFile,destinationFile,"-resize 50x50")
    		'destinationFile = "C:\Temp\newlarge\\" + currUPC + ".jpg"
    		destinationFile = "C:\a5\Ver12web\images\full\\" + currUPC + ".jpg"
    		a5_runImageMagicCommand(sourceFile,destinationFile,"-resize 350x350")
    		file.remove(sourceFile)
    		'Write it back out to the table
    		'debug(1)
    		
    		args.set("argsUPC",currUPC)
    		args.set("argsURL",imURL)
    		args.set("pic_url","http://ugimg.com/images/" + currUPC + ".jpg")
    				
    		sqlStmt = <<%str%
    		 	UPDATE products
    		 	set picture_ref = :argsURL, picture_url = :pic_url,new_field2 = 'Office',upload_by = 'itemmaster'
    			WHERE UPC = :argsUPC;
    			%str%
    
    		flag = cn.Execute(sqlStmt,args)
    		if flag then
    			'ALL OK
    		else
    			'there was an error - close the connection and exit
    			cn.Close()
    			dim msg as c 
    			msg = "Could not execute the query. Error reported was: " + cn.CallResult.text
    			msg = js_escape(msg)
    			dim jscmd as c 
    			jscmd = "alert('" + msg + "');"
    			getItemMaster = jscmd
    			exit function
    		end if  
    	'end if
    	end if
    	delete cf_1
    error_handler:	
    next  
    
    
    cn.close()
    ce.close()
    
    'refresh the UX List control
    'getItemMaster = "{dialog.object}.refreshListData('ItemMasterList');"
    getItemMaster = "alert('All Item Master matching items have been downloaded');"
    Last edited by richardurban; 04-16-2015 at 06:42 PM.
    Richard Urban

    Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

  21. #21
    Member richardurban's Avatar
    Real Name
    Richard Urban
    Join Date
    Apr 2000
    Location
    Harpers Ferry, WV
    Posts
    438

    Default Re: Help with using API and http_fetch

    The error handler above is not correct: The one below is correct:

    Code:
    function getItemMaster as c (e as p)
    	
    'debug(1)
    
    'Here is the MySQL Table structure for the table used in this function
    'SET FOREIGN_KEY_CHECKS=0;
    'DROP TABLE IF EXISTS `tblitemmasterimages`;
    'CREATE TABLE `tblitemmasterimages` (
    '  `ItemId` int(10) NOT NULL AUTO_INCREMENT,
    '  `ItemUPC` varchar(20) DEFAULT NULL,
    '  `ItemDesc` varchar(255) DEFAULT NULL,
    '  `ItemImageURL` varchar(500) DEFAULT NULL,
    '  `ItemImage` longblob,
    '  PRIMARY KEY (`ItemId`)
    ') ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1;
    
    
    dim cn as sql::Connection
    dim flag as l
    dim sqlStmt as c
    dim rs as sql::ResultSet
    dim args as sql::arguments
    
    flag = cn.open("::Name::urbangrocery")
    	args.set("from",e.dataSubmitted.from_code)
    	args.set("to",e.dataSubmitted.to_code)
    
    if flag then
    
    	sqlStmt = "SELECT UPC from products where new_code between :from and :to and (new_field2<>'Office' or new_field2 is null);"
    
    	flag = cn.Execute(sqlStmt,args)
    	if flag then
    		'ALL OK
    		rs = cn.ResultSet
    		myUPCList = rs.ToString()
    	else
    		'there was an error - close the connection and exit
    		cn.Close()
    		dim msg as c 
    		msg = "Could not execute the query. Error reported was: " + cn.CallResult.text
    		msg = js_escape(msg)
    		dim jscmd as c 
    		jscmd = "alert('" + msg + "');"
    		getItemMaster = jscmd
    		exit function
    	end if  
    	
    else
    	'there was an error - close the connection and exit
    	dim msg as c 
    	msg = "Could not open the connection. Error reported was: " + cn.CallResult.text
    	msg = js_escape(msg)
    	dim jscmd as c 
    	jscmd = "alert('" + msg + "');"
    	getItemMaster = jscmd
    	exit function
    end if
    
    'cn.FreeResult()
    
    
    'Set up a list of UPCs to get
    'dim myUPCList as c = <<%str%
    '012000005190
    '012000004926
    '012000008931
    '012000004995
    '078000160468
    '078000156461
    '078000152456
    '078000152463
    '078000003000
    '078000001143
    '%str%
    
    dim imURL as c
    dim imContentXML as c
    dim imContent as p
    dim imCount as n
    dim imName as c
    dim imImage as b
    
    dim flag_1 as l
    dim slist1[0] as c = [ "username:rurban" , "password:Password1" ]
    dim ce as extension::Curl
    ce = extension::Curl.Init()
    ce.setOpt("NOPROGRESS",1)
    ce.setOpt("USERAGENT","curl/7.34.0")
    ce.setOpt("HTTPHEADER",slist1)
    ce.setOpt("MAXREDIRS",50)
    ce.setOpt("CAINFO",a5.Get_Exe_Path()+"\caroot\ca-cert.pem")
    ce.setOpt("CAPATH",a5.Get_Exe_Path()+"\caroot")
    'ce.setOpt("SSH_KNOWNHOSTS","C:\Users\David\AppData\Roaming/_ssh/known_hosts")
    ce.setOpt("TCP_KEEPALIVE",1)
    
    'debug(1)
    for each imItem in myUPCList
    
    	currUPC = imItem.value
    	dim cf_1 as extension::CurlFile	
    	ce.setOpt("URL","https://api.itemmaster.com/v2/item?idx=0&limit=500&upc=" + currUPC + "&epl=600&ef=jpg")	
    	ce.SetOpt("FILE",cf_1)		
    
    	flag_1 = ce.Exec()
    
    	'get the reulting xml from the call
    	imContentXML = cf_1.GetContent()
    
    	'turn the xml into something nice we can use
    	delete imContent
    	dim imContent as p
    	imContent = *property_from_xml(imContentXML,imContent) 
    	
    	imCount = imContent.items.count
    	if imCount <> 0 then
    
    		'we have data from the UPC code
    		
    		'media is an array of images.  Just grab the 1st one.  Another loop is needed to get all images for a product
    		delete imURL
    		dim imURL as c
    		on error goto missing_pic
    		imURL = imContent.items.item.media.medium.url
    	
    		imName = imContent.items.item.name
    	
    		'The returned image url is messed up for our next call because of &amp; which is included
    		'in the returned URL - fix it.
    		imURL = stritran(imURL,"&amp;","&")
    	
    
    		'The URL is a simple http call... use http_get.  The binary image data is returned in the body
    		dim result as p
    		result = http_get(imURL)
    	
    		'The image is binary - write it out to a file... or to a table
    		imImage = result.body
    		
    		'file.from_blob("C:\Temp\tempimage\\" + currUPC + ".jpg", imImage)
    		file.from_blob("C:\a5\Ver12web\tempimages\\" + currUPC + ".jpg", imImage)  
    		'sleep(1)
    		'sourceFile = "C:\Temp\tempimage\\" + currUPC + ".jpg"
    		'destinationFile = "C:\Temp\newimage\\" + currUPC + ".jpg"
    		sourceFile = "C:\a5\Ver12web\tempimages\\" + currUPC + ".jpg"
    		destinationFile = "C:\a5\Ver12web\images\\" + currUPC + ".jpg"
    		
    		a5_runImageMagicCommand(sourceFile,destinationFile,"-resize 50x50")
    		'destinationFile = "C:\Temp\newlarge\\" + currUPC + ".jpg"
    		destinationFile = "C:\a5\Ver12web\images\full\\" + currUPC + ".jpg"
    		a5_runImageMagicCommand(sourceFile,destinationFile,"-resize 350x350")
    		file.remove(sourceFile)
    		'Write it back out to the table
    		'debug(1)
    		
    		args.set("argsUPC",currUPC)
    		args.set("argsURL",imURL)
    		args.set("pic_url","http://ugimg.com/images/" + currUPC + ".jpg")
    				
    		sqlStmt = <<%str%
    		 	UPDATE products
    		 	set picture_ref = :argsURL, picture_url = :pic_url,new_field2 = 'Office',upload_by = 'itemmaster'
    			WHERE UPC = :argsUPC;
    			%str%
    
    		flag = cn.Execute(sqlStmt,args)
    		if flag then
    			'ALL OK
    		else
    			'there was an error - close the connection and exit
    			cn.Close()
    			dim msg as c 
    			msg = "Could not execute the query. Error reported was: " + cn.CallResult.text
    			msg = js_escape(msg)
    			dim jscmd as c 
    			jscmd = "alert('" + msg + "');"
    			getItemMaster = jscmd
    			exit function
    		end if  
    	'end if
    	end if
    	delete cf_1
    	
    next  
    missing_pic:
    resume next
    
    
    cn.close()
    ce.close()
    
    'refresh the UX List control
    'getItemMaster = "{dialog.object}.refreshListData('ItemMasterList');"
    getItemMaster = "alert('All Item Master matching items have been downloaded');"
    
    	
    end function
    Richard Urban

    Grocery Delivery Software for Stores and Entrepreneurs: http://www.urbangrocery.com

Similar Threads

  1. http_fetch() Upload a File
    By kkfin in forum Application Server Version 11 - Web/Browser Applications
    Replies: 0
    Last Post: 10-30-2012, 05:13 PM
  2. http_fetch crashing alpha
    By joellustigman in forum Alpha Five Version 10 - Desktop Applications
    Replies: 3
    Last Post: 11-22-2010, 10:12 PM
  3. HTTP_FETCH() and SOAP
    By David McCormick in forum Alpha Five Version 6
    Replies: 4
    Last Post: 05-09-2010, 07:26 PM
  4. HTTP_POST_PAGE2(), HTTP_FETCH(), or what?
    By Alweth in forum Alpha Five Version 10 - Desktop Applications
    Replies: 5
    Last Post: 03-02-2010, 12:04 PM
  5. http_fetch overlooked
    By jkukuda in forum Alpha Five Version 6
    Replies: 1
    Last Post: 09-27-2005, 12:00 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •