Xero PHP API, getting an error - xero-api

Steps I am following.
I am sending the authenticate parameter as 1 to public.php file and recieving the oauth_token and oauth_token_secret.
http://localhost/XeroOAuth-PHP-master/public.php?authenticate=1
Here at this stage session is generated as below
Array
(
[oauth] => Array
(
[oauth_token] => XXX
[oauth_token_secret] => XXX
[oauth_callback_confirmed] => true
)
)
Next step, I am calling this url http://localhost/XeroOAuth-PHP-master/public.php?oauth_verifier=1&oauth_token=3CHDO7HFISTMVJXDX7IIPMRJUZH2FC
At this stage it throws below error, which says permission_denied.
Error: oauth_problem=permission_denied&oauth_problem_advice=The%20consumer%20was%20denied%20access%20to%20this%20resource.
Below is the entire Error format I receive in the page.
XeroOAuth Object
(
[_xero_defaults] => Array
(
[xero_url] => https://api.xero.com/
[site] => https://api.xero.com
[authorize_url] => https://api.xero.com/oauth/Authorize
[signature_method] => HMAC-SHA1
)
[_xero_consumer_options] => Array
(
[request_token_path] => oauth/RequestToken
[access_token_path] => oauth/AccessToken
[authorize_path] => oauth/Authorize
)
[_action] =>
[_nonce_chars] =>
[params] => Array
(
)
[headers] => Array
(
[Accept] => application/xml
[Content-Length] => 0
[Expect] =>
)
[auto_fixed_time] =>
[buffer] =>
[request_params] => Array
(
)
[_xero_curl_options] => Array
(
[curl_connecttimeout] => 30
[curl_timeout] => 20
[curl_ssl_verifypeer] => 2
[curl_cainfo] => C:\xampp\htdocs\XeroOAuth-PHP-master/certs/ca-bundle.crt
[curl_followlocation] =>
[curl_ssl_verifyhost] => 2
[curl_proxy] =>
[curl_proxyuserpwd] =>
[curl_encoding] =>
[curl_verbose] => 1
)
[config] => Array
(
[xero_url] => https://api.xero.com/
[site] => https://api.xero.com
[authorize_url] => https://api.xero.com/oauth/Authorize
[signature_method] => HMAC-SHA1
[request_token_path] => oauth/RequestToken
[access_token_path] => oauth/AccessToken
[authorize_path] => oauth/Authorize
[curl_connecttimeout] => 30
[curl_timeout] => 20
[curl_ssl_verifypeer] => 2
[curl_cainfo] => C:\xampp\htdocs\XeroOAuth-PHP-master/certs/ca-bundle.crt
[curl_followlocation] =>
[curl_ssl_verifyhost] => 2
[curl_proxy] =>
[curl_proxyuserpwd] =>
[curl_encoding] =>
[curl_verbose] => 1
[application_type] => Public
[oauth_callback] => localhost
[user_agent] => Xero-OAuth-PHP Public
[consumer_key] => XXX
[shared_secret] => XXX
[core_version] => 2.0
[payroll_version] => 1.0
[file_version] => 1.0
[access_token] => XXX
[access_token_secret] => XXX
[host] => https://api.xero.com/oauth/
[multipart] =>
)
[method] => GET
[url] => https://api.xero.com/oauth/AccessToken
[sign] => Array
(
[parameters] => Array
(
[oauth_consumer_key] => YDOVURHNHW7RIHJ384ZYJ7TMVQT8W7
[oauth_nonce] => Ubrb
[oauth_signature_method] => HMAC-SHA1
[oauth_timestamp] => 1504002299
[oauth_token] => 3CHDO7HFISTMVJXDX7IIPMRJUZH2FC
[oauth_verifier] => 1
[oauth_version] => 1.0
[oauth_signature] => o9ucXeTTvA04tQgLTBX5AuMoX2Y=
)
[signature] => o9ucXeTTvA04tQgLTBX5AuMoX2Y%3D
[signed_url] => https://api.xero.com/oauth/AccessToken?oauth_consumer_key=YDOVURHNHW7RIHJ384ZYJ7TMVQT8W7&oauth_nonce=Ubrb&oauth_signature=o9ucXeTTvA04tQgLTBX5AuMoX2Y%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1504002299&oauth_token=3CHDO7HFISTMVJXDX7IIPMRJUZH2FC&oauth_verifier=1&oauth_version=1.0
[header] => OAuth oauth_consumer_key="YDOVURHNHW7RIHJ384ZYJ7TMVQT8W7", oauth_nonce="Ubrb", oauth_signature="o9ucXeTTvA04tQgLTBX5AuMoX2Y%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1504002299", oauth_token="3CHDO7HFISTMVJXDX7IIPMRJUZH2FC", oauth_verifier="1", oauth_version="1.0"
[sbs] => GET&https%3A%2F%2Fapi.xero.com%2Foauth%2FAccessToken&oauth_consumer_key%3DYDOVURHNHW7RIHJ384ZYJ7TMVQT8W7%26oauth_nonce%3DUbrb%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1504002299%26oauth_token%3D3CHDO7HFISTMVJXDX7IIPMRJUZH2FC%26oauth_verifier%3D1%26oauth_version%3D1.0
)
[format] => xml
[response] => Array
(
[headers] => Array
(
[cache_control] => private
[content_type] => text/html; charset=utf-8
[date] => Tue, 29 Aug 2017 10:25:01 GMT
[strict_transport_security] => max-age=31536000
[www_authenticate] => OAuth Realm="10.144.115.64"
[content_length] => 115
[connection] => keep-alive
)
[code] => 401
[response] => oauth_problem=permission_denied&oauth_problem_advice=The%20consumer%20was%20denied%20access%20to%20this%20resource.
[info] => Array
(
[url] => https://api.xero.com/oauth/AccessToken?oauth_consumer_key=YDOVURHNHW7RIHJ384ZYJ7TMVQT8W7&oauth_nonce=Ubrb&oauth_signature=o9ucXeTTvA04tQgLTBX5AuMoX2Y%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1504002299&oauth_token=3CHDO7HFISTMVJXDX7IIPMRJUZH2FC&oauth_verifier=1&oauth_version=1.0
[content_type] => text/html; charset=utf-8
[http_code] => 401
[header_size] => 267
[request_size] => 418
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 1.438
[namelookup_time] => 0
[connect_time] => 0.328
[pretransfer_time] => 1.016
[size_upload] => 0
[size_download] => 115
[speed_download] => 79
[speed_upload] => 0
[download_content_length] => 115
[upload_content_length] => -1
[starttransfer_time] => 1.438
[redirect_time] => 0
[redirect_url] =>
[primary_ip] => 54.209.35.242
[certinfo] => Array
(
)
[primary_port] => 443
[local_ip] => 192.168.1.35
[local_port] => 60675
[request_header] => GET /oauth/AccessToken?oauth_consumer_key=YDOVURHNHW7RIHJ384ZYJ7TMVQT8W7&oauth_nonce=Ubrb&oauth_signature=o9ucXeTTvA04tQgLTBX5AuMoX2Y%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1504002299&oauth_token=3CHDO7HFISTMVJXDX7IIPMRJUZH2FC&oauth_verifier=1&oauth_version=1.0 HTTP/1.1
Host: api.xero.com
User-Agent: Xero-OAuth-PHP Public
Accept-Encoding: deflate, gzip
Accept: application/xml
Content-Length: 0
)
[format] => xml
)
)

This error is returned if you try to swap a request token for an access token when the access token hasn't yet been authorised for an organisation.
There should be a step in the flow of your application which redirects the user to https://api.xero.com/oauth/Authorize?oauth_token=[OAUTH_TOKEN_HERE] - once the Xero user has selected the organisation they're giving access to and given the ok, your second call should work.

Related

How to interpret vin/vout on a Bitcoin transaction?

I used nownodes api to get this response on a transaction:
(
[txid] => 38ac1348219f8aa9349f497bfa6f8d05f775cafbbd26354867d22ca84f15e1e1
[version] => 2
[vin] => Array
(
[0] => stdClass Object
(
[txid] => 38cff250be850140644a1abf022b22f3e59cab15c6091706c3f73218a4d72050
[vout] => 1
[sequence] => 4294967295
[n] => 0
[addresses] => Array
(
[0] => 1EJtMFThodiQ1j26xy1kugFck9325C38CQ
)
[isAddress] => 1
[value] => 158316
[hex] => 4830450221009548f997389fd9881dfed78fe25fcce77f5b96d1545da91dfa13f56db3c02f6e02207fe239ac7916681edc36afd6a9a569981cd12c2c6801616bc15794be5c024431012102afe2e887e3ff56edcebe6b1aa6bf48f4a5a7f02cfc8d743e778896df2054adee
)
)
[vout] => Array
(
[0] => stdClass Object
(
[value] => 7000
[n] => 0
[hex] => a9144a09be6e2917ef00e5d72bc38035db5c5a5ed8ec87
[addresses] => Array
(
[0] => 38SVbkkFE8qcJuGyTJ9fY2cbGS3xknEfYr
)
[isAddress] => 1
)
[1] => stdClass Object
(
[value] => 146856
[n] => 1
[hex] => 76a91491fb9214e6ad9b3ea19c1fd7d3a646e12f804d2688ac
[addresses] => Array
(
[0] => 1EJtMFThodiQ1j26xy1kugFck9325C38CQ
)
[isAddress] => 1
)
)
[blockHash] => 000000000000000000050bfb2a645a5eb7ac882aeb1782bce37ef6a2652c383b
[blockHeight] => 743696
[confirmations] => 8
[blockTime] => 1657005608
[value] => 153856
[valueIn] => 158316
[fees] => 4460
[hex] => 02000000015020d7a41832f7c3061709c615ab9ce5f3222b02bf1a4a64400185be50f2cf38010000006b4830450221009548f997389fd9881dfed78fe25fcce77f5b96d1545da91dfa13f56db3c02f6e02207fe239ac7916681edc36afd6a9a569981cd12c2c6801616bc15794be5c024431012102afe2e887e3ff56edcebe6b1aa6bf48f4a5a7f02cfc8d743e778896df2054adeeffffffff02581b00000000000017a9144a09be6e2917ef00e5d72bc38035db5c5a5ed8ec87a83d0200000000001976a91491fb9214e6ad9b3ea19c1fd7d3a646e12f804d2688ac00000000
)
The transaction was for: 0.0001146 BTC
And the fee was: 0.0000446
Totalling: 0.0001592
However, based on teh result from Nownodes, how can I see what the transaction total was? I can easily see the fee under "Fees". But how do I calculate the actually amount that came in or went out for this transaction?
Thanks!
valueIn = vout[0][value] + vout[1][value] + [fees]
valueIn = 158316
Is that what you have been looking for?

When submitting a place order button to order page that time generate this message

When submitting a place order button to order page that time generate this message.
How to resolve this error. Please help any one.
Order Object ( [id_address_delivery] => 88641 [id_address_invoice] => 88641 [id_shop_group] => 5 [id_shop] => 1 [id_cart] => 31973 [id_currency] => 1 [id_lang] => 1 [id_customer] => 27026 [id_carrier] => 73 [current_state] => [secure_key] => 353b960ec14efca0bf8145f79908794a [payment] => Authorize.net AIM (Advanced Integration Method) [module] => authorizeaim [conversion_rate] => 1.000000 [recyclable] => 0 [gift] => 0 [gift_message] => [mobile_theme] => 0 [shipping_number] => [total_discounts] => 0 [total_discounts_tax_incl] => 0 [total_discounts_tax_excl] => 0 [total_paid] => 6.5 [total_paid_tax_incl] => 6.5 [total_paid_tax_excl] => 6.5 [total_paid_real] => 0 [total_products] => 6.5 [total_products_wt] => 6.5 [total_shipping] => 0 [total_shipping_tax_incl] => 0 [total_shipping_tax_excl] => 0 [carrier_tax_rate] => 0 [hazmat_fees] => 0 [poison_fees] => 0 [refrigerated_fees] => 0 [ponumber] => [total_wrapping] => 0 [total_wrapping_tax_incl] => 0 [total_wrapping_tax_excl] => 0 [invoice_number] => [delivery_number] => [invoice_date] => 0000-00-00 00:00:00 [delivery_date] => 0000-00-00 00:00:00 [valid] => [date_add] => [date_upd] => [reference] => JYADOFQPY [webserviceParameters:protected] => Array ( [objectMethods] => Array ( [add] => addWs ) [objectNodeName] => order [objectsNodeName] => orders [fields] => Array ( [id_address_delivery] => Array ( [xlink_resource] => addresses ) [id_address_invoice] => Array ( [xlink_resource] => addresses ) [id_cart] => Array ( [xlink_resource] => carts ) [id_currency] => Array ( [xlink_resource] => currencies ) [id_lang] => Array ( [xlink_resource] => languages ) [id_customer] => Array ( [xlink_resource] => customers ) [id_carrier] => Array ( [xlink_resource] => carriers ) [current_state] => Array ( [xlink_resource] => order_states ) [module] => Array ( [required] => 1 ) [invoice_number] => Array ( ) [invoice_date] => Array ( ) [delivery_number] => Array ( ) [delivery_date] => Array ( ) [valid] => Array ( ) [date_add] => Array ( ) [date_upd] => Array ( ) ) [associations] => Array ( [order_rows] => Array ( [resource] => order_row [setter] => [virtual_entity] => 1 [fields] => Array ( [id] => Array ( ) [product_id] => Array ( [required] => 1 ) [product_attribute_id] => Array ( [required] => 1 ) [product_quantity] => Array ( [required] => 1 ) [product_name] => Array ( [setter] => ) [product_price] => Array ( [setter] => ) [unit_price_tax_incl] => Array ( [setter] => ) [unit_price_tax_excl] => Array ( [setter] => ) ) ) ) ) [_taxCalculationMethod:protected] => 1 [id] => [id_shop_list] => [get_shop_from_context:protected] => 1 [table:protected] => orders [identifier:protected] => id_order [fieldsRequired:protected] => Array ( [0] => id_address_delivery [1] => id_address_invoice [2] => id_cart [3] => id_currency [4] => id_lang [5] => id_customer [6] => id_carrier [7] => payment [8] => module [9] => total_paid [10] => total_paid_real [11] => total_products [12] => total_products_wt [13] => conversion_rate ) [fieldsSize:protected] => Array ( ) [fieldsValidate:protected] => Array ( [id_address_delivery] => isUnsignedId [id_address_invoice] => isUnsignedId [id_cart] => isUnsignedId [id_currency] => isUnsignedId [id_shop_group] => isUnsignedId [id_shop] => isUnsignedId [id_lang] => isUnsignedId [id_customer] => isUnsignedId [id_carrier] => isUnsignedId [current_state] => isUnsignedId [secure_key] => isMd5 [payment] => isGenericName [module] => isModuleName [recyclable] => isBool [gift] => isBool [gift_message] => isMessage [mobile_theme] => isBool [total_discounts] => isPrice [total_discounts_tax_incl] => isPrice [total_discounts_tax_excl] => isPrice [total_paid] => isPrice [total_paid_tax_incl] => isPrice [total_paid_tax_excl] => isPrice [total_paid_real] => isPrice [total_products] => isPrice [total_products_wt] => isPrice [total_shipping] => isPrice [total_shipping_tax_incl] => isPrice [total_shipping_tax_excl] => isPrice [carrier_tax_rate] => isFloat [hazmat_fees] => isPrice [poison_fees] => isPrice [refrigerated_fees] => isPrice [total_wrapping] => isPrice [total_wrapping_tax_incl] => isPrice [total_wrapping_tax_excl] => isPrice [shipping_number] => isTrackingNumber [conversion_rate] => isFloat [date_add] => isDate [date_upd] => isDate ) [fieldsRequiredLang:protected] => Array ( ) [fieldsSizeLang:protected] => Array ( ) [fieldsValidateLang:protected] => Array ( ) [tables:protected] => Array ( ) [image_dir:protected] => [image_format:protected] => jpg [def:protected] => Array ( [table] => orders [primary] => id_order [fields] => Array ( [id_address_delivery] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_address_invoice] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_cart] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_currency] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_shop_group] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_shop] => Array ( [type] => 1 [validate] => isUnsignedId ) [id_lang] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_customer] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [id_carrier] => Array ( [type] => 1 [validate] => isUnsignedId [required] => 1 ) [current_state] => Array ( [type] => 1 [validate] => isUnsignedId ) [secure_key] => Array ( [type] => 3 [validate] => isMd5 ) [payment] => Array ( [type] => 3 [validate] => isGenericName [required] => 1 ) [module] => Array ( [type] => 3 [validate] => isModuleName [required] => 1 ) [recyclable] => Array ( [type] => 2 [validate] => isBool ) [gift] => Array ( [type] => 2 [validate] => isBool ) [gift_message] => Array ( [type] => 3 [validate] => isMessage ) [mobile_theme] => Array ( [type] => 2 [validate] => isBool ) [total_discounts] => Array ( [type] => 4 [validate] => isPrice ) [total_discounts_tax_incl] => Array ( [type] => 4 [validate] => isPrice ) [total_discounts_tax_excl] => Array ( [type] => 4 [validate] => isPrice ) [total_paid] => Array ( [type] => 4 [validate] => isPrice [required] => 1 ) [total_paid_tax_incl] => Array ( [type] => 4 [validate] => isPrice ) [total_paid_tax_excl] => Array ( [type] => 4 [validate] => isPrice ) [total_paid_real] => Array ( [type] => 4 [validate] => isPrice [required] => 1 ) [total_products] => Array ( [type] => 4 [validate] => isPrice [required] => 1 ) [total_products_wt] => Array ( [type] => 4 [validate] => isPrice [required] => 1 ) [total_shipping] => Array ( [type] => 4 [validate] => isPrice ) [total_shipping_tax_incl] => Array ( [type] => 4 [validate] => isPrice ) [total_shipping_tax_excl] => Array ( [type] => 4 [validate] => isPrice ) [carrier_tax_rate] => Array ( [type] => 4 [validate] => isFloat ) [hazmat_fees] => Array ( [type] => 4 [validate] => isPrice ) [poison_fees] => Array ( [type] => 4 [validate] => isPrice ) [refrigerated_fees] => Array ( [type] => 4 [validate] => isPrice ) [ponumber] => Array ( [type] => 3 ) [total_wrapping] => Array ( [type] => 4 [validate] => isPrice ) [total_wrapping_tax_incl] => Array ( [type] => 4 [validate] => isPrice ) [total_wrapping_tax_excl] => Array ( [type] => 4 [validate] => isPrice ) [shipping_number] => Array ( [type] => 3 [validate] => isTrackingNumber ) [conversion_rate] => Array ( [type] => 4 [validate] => isFloat [required] => 1 ) [invoice_number] => Array ( [type] => 1 ) [delivery_number] => Array ( [type] => 1 ) [invoice_date] => Array ( [type] => 5 ) [delivery_date] => Array ( [type] => 5 ) [valid] => Array ( [type] => 2 ) [reference] => Array ( [type] => 3 ) [date_add] => Array ( [type] => 5 [validate] => isDate ) [date_upd] => Array ( [type] => 5 [validate] => isDate ) ) [classname] => Order ) [update_fields:protected] => [product_list] => Array ( [0] => Array ( [hazmat_fees] => 0 [poison_pack] => 0 [refrigerated_fees] => 0 [drop_shipping] => 0 [id_product_attribute] => 0 [id_product] => 21930 [cart_quantity] => 1 [id_shop] => 1 [name] => Hot/cold Protective Mitt, 17" Length [is_virtual] => 0 [description_short] =>
• For extreme hot or cold applications
• Cotton coated with aluminized silicones
• Withstands 450°F in short int
[available_now] => [available_later] => [id_category_default] => 1181 [id_supplier] => 0 [id_manufacturer] => 0 [on_sale] => 0 [ecotax] => 0.000000 [additional_shipping_cost] => 0.00 [available_for_order] => 1 [price] => 6.5 [active] => 1 [unity] => [unit_price_ratio] => 0.000000 [quantity_available] => 106 [width] => 8.000000 [height] => 8.000000 [depth] => 8.000000 [out_of_stock] => 2 [weight] => 1.000000 [date_add] => 0000-00-00 00:00:00 [date_upd] => 2016-09-01 08:00:39 [quantity] => 1 [link_rewrite] => HOTCOLD-PROTECTIVE-MITT-17-LENGTH [category] => chemical-and-heat-gloves [unique_id] => 0000021930000000000088641 [id_address_delivery] => 88641 [wholesale_price] => 4.214648 [advanced_stock_management] => 0 [supplier_reference] => [id_customization] => [customization_quantity] => [reference] => 180-50591 [ean13] => [upc] => [minimal_quantity] => 1 [stock_quantity] => 106 [price_wt] => 6.5 [total_wt] => 6.5 [total] => 6.5 [id_image] => 21930-71813 [legend] => [reduction_applies] => [quantity_discount_applies] => [allow_oosp] => 1 [features] => Array ( ) [rate] => 0 [tax_name] => [warehouse_list] => Array ( [0] => ) [in_stock] => 1 [carrier_list] => Array ( [0] => 73 ) ) ) )
This is the ggetto order, this message appears in the presence of a:
var_dump ($ order);
in any file should appear that function.

Not getting the TOKEN after the success of recurring subscription payment

Array
(
[txn_type] => subscr_signup
[subscr_id] => I-WT2XTR8FF1NX
[last_name] => testing
[residence_country] => US
[mc_currency] => USD
[item_name] => ilookin's monthly
[business] => indhumathi.k-facilitator#optisolbusiness.com
[amount3] => 1.00
[recurring] => 0
[address_street] => 1 Main St
[payer_status] => verified
[payer_email] => optisol#optisol.com
[address_status] => confirmed
[first_name] => optisol
[receiver_email] => indhumathi.k-facilitator#optisolbusiness.com
[address_country_code] => US
[payer_id] => QUMFGMGW2N4Z2
[address_city] => San Jose
[reattempt] => 1
[address_state] => CA
[subscr_date] => 01:29:58 Apr 19, 2016 PDT
[address_zip] => 95131
[charset] => windows-1252
[address_country] => United States
[period3] => 1 M
[mc_amount3] => 1.00
[address_name] => optisol testing
[auth] => A0LUGRBFj6LNkWG.xKRjssSW6CnDDxfiPEt6gNkXkpQz7wuKMdiuZAm3B56YywWHBW9Hjc07tetTpy8sD8YAMdQ
[form_charset] => UTF-8
[EC-1A5481165X7875926] =>
)
Am getting these parameters.But I need token after the success completion of recurring subscription.How to get it after the payment success?
Please Advice
Thanks in Advance.

Parse a json array in PHP to get the required values

I am using a API provided by this website
http://pnrapi.alagu.net/
By using this API, we can get PNR status of our indian railways.
I am using CURL to make a call and get the page content which is something like this, in an array format:
Array ( [url] => http://pnrapi.alagu.net/api/v1.0/pnr/4563869832 [content_type] => application/json;charset=utf-8 [http_code] => 200 [header_size] => 185 [request_size] => 130 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 0 [total_time] => 2.906 [namelookup_time] => 0 [connect_time] => 0.312 [pretransfer_time] => 0.312 [size_upload] => 0 [size_download] => 548 [speed_download] => 188 [speed_upload] => 0 [download_content_length] => 548 [upload_content_length] => 0 [starttransfer_time] => 2.906 [redirect_time] => 0 [certinfo] => Array ( ) [primary_ip] => 50.57.204.234 [primary_port] => 80 [local_ip] => 192.168.1.10 [local_port] => 60105 [redirect_url] => [errno] => 0 [errmsg] => [content] => {"status":"OK","data":{"train_number":"16178","chart_prepared":false,"pnr_number":"4563869832","train_name":"ROCKFORT EXPRES","travel_date":{"timestamp":1369506600,"date":"26-5-2013"},"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}} )
but when I go to the URL http://pnrapi.alagu.net/api/v1.0/pnr/4563869832 , it gives me output as shown below:
{"status":"OK","data":{"train_number":"16178","chart_prepared":false,"pnr_number":"4563869832","train_name":"ROCKFORT EXPRES","travel_date":{"timestamp":1369506600,"date":"26-5-2013"},"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}}
Now, it seems that output on my web page with curl have got some extra text which is in the start as you can see both the outputs above.
Well, my question is, how can I get the values from above array.
I am talking about the array output which I'm getting on my page using CURL, which looks like this:
Array (
[url] => http://pnrapi.alagu.net/api/v1.0/pnr/4563869832
[content_type] => application/json;charset=utf-8
[http_code] => 200
[header_size] => 185
[request_size] => 130
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 2.906
[namelookup_time] => 0
[connect_time] => 0.312
[pretransfer_time] => 0.312
[size_upload] => 0
[size_download] => 548
[speed_download] => 188
[speed_upload] => 0
[download_content_length] => 548
[upload_content_length] => 0
[starttransfer_time] => 2.906
[redirect_time] => 0
[certinfo] => Array ( )
[primary_ip] => 50.57.204.234
[primary_port] => 80
[local_ip] => 192.168.1.10
[local_port] => 60105
[redirect_url] =>
[errno] => 0
[errmsg] => [content] => {"status":"OK","data":{"train_number":"16178","chart_prepared":false,"pnr_number":"4563869832","train_name":"ROCKFORT EXPRES","travel_date":{"timestamp":1369506600,"date":"26-5-2013"},"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}} )
Code in my PHP page is:
<?php
function get_web_page( $url )
{
$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 => "spider", // 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
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
}
$pnr = get_web_page('http://pnrapi.alagu.net/api/v1.0/pnr/4563869832');
echo "<code>";
print_r($pnr);
echo "</code>";
?>
I only need the values under "content" which are train number, train name, travel date etc.
So, what would be best way to extract this information into each variable?
Like I want it like this:
$train_no = [some code];
$train_name = [some_code];
and so on...
Thanks in advance.
I tried this:
echo $pnr['content'];
and the output I got is:
{"status":"OK",
"data":"train_number":"16178",
"chart_prepared":false,
"pnr_number":"4563869832",
"train_name":"ROCKFORT EXPRES",
"travel_date":{"timestamp":1369506600,"date":"26-5-2013"},
"from":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20"},
"to":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},
"alight":{"code":"MS","name":"CHENNAI EGMORE","time":"05:15"},
"board":{"code":"TPJ","name":"TIRUCHIRAPPALLI JUNCTION","time":"22:20","timestamp":1369587000},
"class":"2A","passenger":[{"seat_number":"W/L 39,RLGN","status":"W/L 27"}]}}
Now can any one give me an idea about how can I fetch unique values from above array?
I'm not sure where the JSON string is. But let's say it's the $pnr variable.
$json = json_decode($pnr, true);
$train_no = $json["data"]["train_number"];
$train_name = $json["data"]["train_name"];
Updated:
If you don't need all the other things you can do something like the following:
$npr = file_get_contents(url);
and then run the code above.
You're looking through the header, where you should be looking at the content. Return $content instead in your function and then you can parse out the response:
function get_web_page( $url ) {
$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 => "spider", // 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
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = json_decode( curl_exec( $ch ) );
curl_close( $ch );
return array(
'train_no' => $content->data->train_number,
'train_name' => $content->data->train_name,
);
}
$pnr = get_web_page('http://pnrapi.alagu.net/api/v1.0/pnr/4563869832');
echo "<pre>" . print_r($pnr, true) . "</pre>";

cakephp how to group file data when upload multiple file

I use cakephp FormHelper to generate html form code.
echo $this->Form->create('newGallery.', array('type'=>'file'));
echo $this->Form->input('Photos',array(
'type' => 'file',
'label' => 'Photos (jpg,png,gif)',
'name' => 'upload[]',
'required',
'multiple'
));
echo $this->Form->end('Create');
but result is
Array
(
[upload] => Array
(
[name] => Array
(
[0] => Pic1.jpg
[1] => Pic2.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:\wamp\tmp\phpB073.tmp
[1] => C:\wamp\tmp\phpB084.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 216302
[1] => 107102
)
)
)
I need to group the data of the same file so that i can deal with by using foreach loop. This is the result i need
Array
(
[upload] => Array
(
[0] => Array
(
[name] => Pic1.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\phpB073.tmp
[error] => 0
[size] => 216302
)
[1] => Array
(
[name] => Pic2.jpg
[type] => image/jpeg
[tmp_name] => C:\wamp\tmp\phpB084.tmp
[error] => 0
[size] => 107102
)
)
)
Thank you indeed for your help.
I think this can help you :)
/* create new empty array */
$result=array();
/* create new index */
$i=0;
/* foreach in your $upload or other array */
foreach($upload['upload'] as $key => $val){
foreach($val as $sub_val){
$result[$i][$key]=$sub_val;
$i++;
}
$i=0;
}