Php-based stock data interface call code example-PHP source code

Source: Internet
Author: User
Php-based stock data interface call code instance php code

// + Response // ---------------------------------- // sample code for calling stock data-aggregate data // ---------------------------------- header ('content-type: text/html; charset = utf-8 '); // Configure Your applied appkey $ appkey = "*********************"; // ************* 1. shanghai and Shenzhen stock markets ************ $ url =" http://web.juhe.cn:8080/finance/stock/hs "; $ Params = array (" gid "=>" ", // stock number. the Shanghai stock market starts with sh. the Shenzhen stock market starts with sz, for example: sh601009 "key" => $ appkey, // APP Key); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring ); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0 ') {print_r ($ result);} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * *********** // *************** 2. hong Kong stock market ************ $ url =" http://web.juhe.cn:8080/finance/stock/hk "; $ Params = array (" num "=>" ", // the stock code. for example, 00001 is the stock code" key "=> $ appkey, // APP Key); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring); $ result = json_decode ($ content, true ); if ($ result) {if ($ result ['error _ Code'] = '0') {print_r ($ result );} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * *********** // *************** 3. u.S. stock market ************ $ url =" http://web.juhe.cn:8080/finance/stock/usa "; $ Params = array (" gid "=>" ", // the stock code. for example, aapl is the stock code of" apple "" key "=> $ appkey, // APP Key); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring); $ result = json_decode ($ content, true ); if ($ result) {if ($ result ['error _ Code'] = '0') {print_r ($ result );} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * *********** // *************** 4. hong Kong stock list ************ $ url =" http://web.juhe.cn:8080/finance/stock/hk All "; $ params = array (" key "=> $ appkey, // page of the APPKEY you applied for" page "=>" ", // page number, 20 pieces of data per page, default 1st pages); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0') {print_r ($ result );} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * *********** // **************** 5. u.S. stock list ************ $ url =" http://web.juhe.cn:8080/finance/stock/usa All "; $ params = array (" key "=> $ appkey, // page of the APPKEY you applied for" page "=>" ", // page number, 20 pieces of data per page, default 1st pages); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0') {print_r ($ result );} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * *********** // *************** 6. shenzhen stock market list ************ $ url =" http://web.juhe.cn:8080/finance/stock/szall "; $ Params = array (" key "=> $ appkey, // The APPKEY you applied for" page "=> "", // page (20 pieces of data per page), default: 1st pages); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring ); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0 ') {print_r ($ result);} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * *********** // *************** 7. shanghai stock list ************ $ url =" http://web.juhe.cn:8080/finance/stock/shall "; $ Params = array (" key "=> $ appkey, // page of the APPKEY you applied for" page "=>" ", // page number, 20 pieces of data per page, default 1st pages); $ paramstring = http_build_query ($ params); $ content = juhecurl ($ url, $ paramstring); $ result = json_decode ($ content, true); if ($ result) {if ($ result ['error _ Code'] = '0') {print_r ($ result );} else {echo $ result ['error _ Code']. ":". $ result ['reason '] ;}} else {echo "request failed ";} //************************************** * ***********/*** please Request the returned content of the interface * @ param string $ url [requested URL] * @ param string $ params [request parameters] * @ param int $ ipost [whether POST is used] * @ return string */function juhecurl ($ url, $ params = false, $ ispost = 0) {$ httpInfo = array (); $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt ($ ch, CURLOPT_USERAGENT, 'juhedata'); curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 60); curl_setopt ($ ch, CU RLOPT_TIMEOUT, 60); curl_setopt ($ ch, success, true); curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, true); if ($ ispost) {curl_setopt ($ ch, CURLOPT_POST, true); curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ params); curl_setopt ($ ch, CURLOPT_URL, $ url);} else {if ($ params) {curl_setopt ($ ch, CURLOPT_URL, $ url. '? '. $ Params);} else {curl_setopt ($ ch, CURLOPT_URL, $ url) ;}}$ response = curl_exec ($ ch); if ($ response === FALSE) {// echo "cURL Error :". curl_error ($ ch); return false;} $ httpCode = curl_getinfo ($ ch, CURLINFO_HTTP_CODE); $ httpInfo = array_merge ($ httpInfo, curl_getinfo ($ ch )); curl_close ($ ch); return $ response ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.